Skip to content

Instantly share code, notes, and snippets.

@makersacademy
Created March 6, 2013 10:11
Show Gist options
  • Save makersacademy/5098294 to your computer and use it in GitHub Desktop.
Save makersacademy/5098294 to your computer and use it in GitHub Desktop.
Blocks examples
#!/usr/bin/env ruby
def each(array)
array.length.times do |i|
yield array[i]
end
end
def select(array)
result = []
array.length.times do |i|
result << array[i] if yield(array[i])
end
result
end
array = [0,1,2,3]
# each(array) do |element|
# puts element
# end
odds = select(array) do |element|
element.odd?
end
puts odds
starbucks = Starbucks.new
def latte(number)
# ...make latte and put all cups in all_cups
all_cups.each do |latte|
yield latte
end
all_cups
end
starbucks.latte(10) do |latte|
latte.add_cream! if latte.number < 5
end
bikes.count {|bike| bike.broken?}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment