Created
September 30, 2013 21:35
-
-
Save loganhasson/6770645 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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. | |
# apple_picker(["apple", "orange", "apple"]) #=> ["apple", "apple"] | |
# with collect | |
# def apple_picker(fruit_array) | |
# fruit_array.collect do |fruit| | |
# fruit if fruit == "apple" | |
# end.compact | |
# end | |
# puts apple_picker(["apple", "orange", "apple"]) | |
# with select | |
def apple_picker(fruit_array) | |
fruit_array.select { |fruit| fruit == "apple" } | |
end | |
puts apple_picker(["apple", "orange", "apple"]) | |
# .collect returns something for each element of the array, whereas .select | |
# only returns items from the array that match the condition set within | |
# the block that is passed to it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment