4

Possible Duplicate:
What does map(&:name) mean in Ruby?

I was watching railscasts more virtual attributes episode. In that episode, at one point, ryan used a map method syntax which I am not able to understand, Could someone please explain it?

tags.map(&:name).join(' ')

tags is an object of Tag Model, which has a name attribute. I am able to understand the meaning of this(I think so :)). All the tag object's name attribute are retrieved as an array and joined based on the ' '. But whats the deal with &:name

Thanks

Community
  • 1
  • 1
felix
  • 11,304
  • 13
  • 69
  • 95
  • 2
    BTW: The &:name syntax only works with Ruby >=1.9 or ActiveSupport. – Simon Woker Mar 08 '11 at 11:43
  • 4
    @Mark, That's incorrect. It is in 1.8.7 core - http://www.ruby-doc.org/core-1.8.7/classes/Symbol.html#M000386 – idlefingers Mar 08 '11 at 12:58
  • 2
    This is a duplicate of no less than 12 other questions that have already been asked and answered here on StackOverflow: [Understanding \[ClassOne, ClassTwo\].each\(&:my_method\)](http://StackOverflow.Com/q/99318/), [What does `map(&:name)` mean in Ruby?](http://StackOverflow.Com/q/1217088/), [What exactly is is this in ruby: `&:capitalize`](http://StackOverflow.Com/q/1792683/), [Ruby/Ruby on Rails ampersand colon shortcut](http://StackOverflow.Com/q/1961030/), [Ruby : `&:symbol` syntax](http://StackOverflow.Com/q/2096975/), … – Jörg W Mittag Mar 08 '11 at 17:53
  • 1
    … [What is this `&:last` Ruby Construct Called?](http://StackOverflow.Com/q/2211751/), [What do you call the `&:` operator in Ruby?](http://StackOverflow.Com/q/2259775/), [What does `map(&:name)` do in this Ruby code?](http://StackOverflow.Com/q/2388337/), [What are `:+` and `&+` in ruby?](http://StackOverflow.Com/q/2697024/), [`&:views_count` in `Post.published.collect(&:views_count)`](http://StackOverflow.Com/q/3888044/), [Ruby Proc Syntax](http://StackOverflow.Com/q/4512587/), [How does “`(1..4).inject(&:+)`” work in Ruby](http://StackOverflow.Com/q/5003257/). – Jörg W Mittag Mar 08 '11 at 17:54
  • @Jörg Probably belongs here: http://meta.stackexchange.com/questions/9686/what-is-the-most-rampant-duplicate-on-stack-overflow – Jakub Hampl Mar 09 '11 at 17:33
  • @Jakub Hampl: The problem is that StackOverflow's search is fundamentally broken, but the developers not only don't fix it, they don't even understand *that* it is broken. [Try searching for `&:` and you'll see what I mean.](http://StackOverflow.Com/search?q=%26%3A) This bug was first reported years ago, and many times since. – Jörg W Mittag Mar 10 '11 at 00:36
  • @Jörg Yeah I just posted on meta something about it, php's got the same problem with the `=&` operator there - also a slightly not-so-intuitive feature that has a gazilion duplicates. – Jakub Hampl Mar 10 '11 at 00:41

2 Answers2

9

The & is a shortcut to Symbol#to_proc which will convert the symbol you pass to it to a method name on the object. So &:name converts to { |reciever| receiever.name } which is then passed to the map method.

It's a great way to make your code a lot more concise and avoid having tons of blocks all over the place.

Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106
6

It's shorthand for tags.map(:name.to_proc) which is like calling tags.map{|tag| tag.name } and just collects all the tag names into an array.

idlefingers
  • 31,659
  • 5
  • 82
  • 68