Skip to content

Instantly share code, notes, and snippets.

@samtalks
Created October 1, 2013 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samtalks/6778283 to your computer and use it in GitHub Desktop.
Save samtalks/6778283 to your computer and use it in GitHub Desktop.
# 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