Skip to content

Instantly share code, notes, and snippets.

@matthewrudy
Forked from gerhard/gist:42537
Created January 2, 2009 14:58
Show Gist options
  • Save matthewrudy/42552 to your computer and use it in GitHub Desktop.
Save matthewrudy/42552 to your computer and use it in GitHub Desktop.
# 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