Created
March 6, 2013 10:11
-
-
Save makersacademy/5098294 to your computer and use it in GitHub Desktop.
Blocks examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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