Created
October 1, 2013 13:18
-
-
Save samtalks/6778283 to your computer and use it in GitHub Desktop.
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
# Apple picker | |
# Create a method, apple_picker, that will pick all the apples out of an array. Implement it with collect and then implement it with select. Write a sentence about how select differs from collect. | |
def apple_picker(fruits) | |
answer = [] | |
fruits.collect do |fruit| | |
answer << fruit if fruit == "apple" | |
end | |
puts answer | |
end | |
# select is a collect + a conditional all in one | |
def apple_picker(fruits) | |
answer = fruits.select {|fruit| fruit == "apple"} | |
puts answer | |
end | |
apple_picker(["apple", "orange", "apple"]) #=> ["apple", "apple"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment