Created
September 30, 2013 21:29
-
-
Save mecampbellsoup/6770565 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
| 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