Skip to content

Instantly share code, notes, and snippets.

View ashleygwilliams's full-sized avatar

ashley williams ashleygwilliams

  • 07:15 (UTC -05:00)
View GitHub Profile
@ashleygwilliams
ashleygwilliams / middleware.md
Last active December 18, 2015 18:29
rack lab

Rack runs an instance of a class that implements the #call method, but it can also use several other "stand-alone" Rack apps before returning the response to your web-browser by passing the status, headers, and body to each new app, we can stack middleware between the server and the browser to manipulate the details of the request/response cycle. Think of it like a stack.

  1. Create a directory called rack_middleware

  2. cd into that directory and create the file app.rb

  3. We're going to define our base Rack app in app.rb. We just need a class that implements a call method. Add a MyApp class that says hello, but doesn't set any headers. We'll add those later.

# app.rb
@ashleygwilliams
ashleygwilliams / hellorack.rb
Last active December 18, 2015 17:19
a simple exercise to explore the code required to run a simple local helloworld rack app
require 'rack'
class MyApp #change this name
def call(env)
#some code goes here!
end
end
Rack::Handler::WEBrick.run(MyApp.new, {:Port => 3000}) #you'll need to update this...
@ashleygwilliams
ashleygwilliams / nutrition.rb
Last active December 18, 2015 16:19
homework for alex
#fill out this meal array with items and details hashes. make sure you have at least 5 items
ITEMS = [ "ITEM" => {:calories=> ??, :fat => ??, :carbs => ??, :protein=>},
"ITEM" => {:calories=> ??, :fat => ??, :carbs => ??, :protein=>}
]
#here is a method that randomly makes you eat x portions of the food
def generatePortions
portions = []
rand(20).times do
@ashleygwilliams
ashleygwilliams / enumerable.rb
Created June 19, 2013 00:02
practice using nested data structures with each
# create a new hash that has this structure
# {
# "A" => "a"
# "B" => "b"
# ...
# "Z" => "z"
#}
#using a single each loop
#know that you can do (A..Z) and (a..z)
@ashleygwilliams
ashleygwilliams / gist:5778813
Created June 14, 2013 01:37
HOMEWORK: ALEX + JULIAN 13 JUNE 2013
JULIAN:
finish scraper for two types of site
read http://www.w3schools.com/sql/ << teach you how to build sql tables
port the scraper lib from the sinatra app to the rails app
pay attention to what your models are!!
tables = > columns :: models => attributes
what data am i scraping || what attributes does my model have || what columns should my table have
SCRAPER || RAILS || SQL
@ashleygwilliams
ashleygwilliams / flow.rb
Last active December 18, 2015 11:29
control flow wat -> control flow WUT
def step1(x)
if x==1
puts "it was all a dream"
end
return 2
end
def step2(x)
if x==2
@ashleygwilliams
ashleygwilliams / gist:5773822
Created June 13, 2013 13:51
notes from my talk about how to best approach the enumerable labs and complex projects in general
two things i want to accomplish in the next hour:
+ clarify the instructions for the enumerable labs
+ teach you guys about how to break problems down (in words)
+ in code (methods are your friend)
first things first:
+ how does everyone feel right now?
+ walls happen, perservere
+ when you don't understand the instructions, make your own and keep going because the solution is likely not going to be as different as if you had
@ashleygwilliams
ashleygwilliams / gist:5762354
Created June 12, 2013 02:09
notes about arrays and hashes for alex
myArray = [1,2,3,4,5]
myArray[0] = 1
myArray[1] = 2
myArray.each do |arr|
puts arr
end
myHash = {"a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5}
#ITEMS = [ {"AVOCADO" => {:price => 3.00, :clearance => true}},
# {"KALE" => {:price => 3.00,:clearance => false}},
# {"BLACK_BEANS" => {:price => 2.50,:clearance => false}},
# {"ALMONDS" => {:price => 9.00, :clearance => false}},
# {"TEMPEH" => {:price => 3.00,:clearance => true}},
# {"CHEESE" => {:price => 6.50,:clearance => false}},
# {"BEER" => {:price => 13.00, :clearance => false}},
# {"PEANUTBUTTER" => {:price => 3.00,:clearance => true}},
# {"BEETS" => {:price => 2.50,:clearance => false}}]
#
##create a hash that includes counts as a key for each item
myCart.each do |item|
myNewCart[item]||= {} #[{item}, {item}] => {{item}=>{}, {item}=>{}}
myNewCart[item][:count] ||= 0 #{{item}=>{:count=>0}, {item}=>{:count=>0}}
myNewCart[item][:count] += 1 #{{item}=>{:count=>+=1}, {item}=>{:count=>+=1}}
end