-
-
Save matthewrudy/42552 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
# each approach | |
locations = [] | |
stocks.each {|stock| locations << stock.location} | |
return locations | |
# refactored with inject | |
stocks.inject([]) {|locations, stock| locations << stock.location} | |
# the correct approach | |
stocks.map {|stock| stock.location} | |
# or with Rails' symbol to_proc | |
stocks.map(&:location) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment