Created
March 30, 2011 17:48
-
-
Save jmccartie/894867 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
# How I might do it 3 months ago in a different language | |
n = 0 | |
item.categories.each do |category| | |
puts category.title | |
break if n == item.categories.count | |
puts ", " | |
n += 1 | |
end | |
# How I tried it first in ruby | |
names = item.categories.collect {|category| category.title} | |
puts names.join(", ") | |
# Oh, I can method-chain that? | |
puts item.categories.collect {|category| category.title}.join(", ") | |
# Then, Ryan Waldron (@erebor) swoops in and blows my mind | |
puts item.categories.collect(&:title).join(", ") | |
# Mind blown | |
# Symbol#to_proc | |
# http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment