Skip to content

Instantly share code, notes, and snippets.

@mecampbellsoup
Created September 30, 2013 21:29
Show Gist options
  • Select an option

  • Save mecampbellsoup/6770565 to your computer and use it in GitHub Desktop.

Select an option

Save mecampbellsoup/6770565 to your computer and use it in GitHub Desktop.
def apple_picker(array_of_fruits)
#array_of_fruits.select { |fruit| fruit if fruit == "apple" }
array_of_fruits.collect do |fruit|
fruit if fruit == "apple"
end.compact
end
p apple_picker(["apple", "orange", "apple"]) #=> ["apple", "apple"]
# select vs. collect
# if a conditional requirement is not met within a collect iteration, the value returned
# is equal to nil... in other words, select can and will return an array with fewer
# values than that of self (on which select was called)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment