7

Possible Duplicate:
What does map(&:name) mean in Ruby?
Ruby/Ruby on Rails ampersand colon shortcut

For example,

contacts.sort_by(&:first_name).

I understand what this does, but I dont understand the &: notations, what does that mean, is it a symbol(:) with a block (&)? Where can I read more about it?

Community
  • 1
  • 1
Kamilski81
  • 14,409
  • 33
  • 108
  • 161
  • 2
    This is a duplicate of no less than 16 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 `&:capitalize` in Ruby?](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 Feb 08 '12 at 09:25
  • 2
    … [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 Feb 08 '12 at 09:26
  • 2
    … [What does following statement `&:property` ?](http://StackOverflow.Com/q/5620411/), [What does the `&` mean in the following ruby syntax?](http://StackOverflow.Com/q/5952175/), [Why would one use the unary operator on a property in ruby? i.e `&:first`](http://StackOverflow.Com/q/6289084/), and [how does `Array#map` have parameter to do something like this?](http://StackOverflow.Com/q/6716629/). – Jörg W Mittag Feb 08 '12 at 09:26
  • Sorry, believe it or not I searched for this all over and couldnt find the answer...sorry! – Kamilski81 Feb 08 '12 at 14:36

2 Answers2

9

When & used before Proc object in method invocation, it treats the Proc as if it was an ordinary block following the invocation.
When & used before other type of object (symbol :first_name in your case) in method invocation, it tries to call to_proc on this object and if it does not have to_proc method you will get TypeError.

Generally &:first_name is the same as &:first_name.to_proc.

Symbol#to_proc Returns a Proc object which respond to the given method by sym.

:first_name.to_proc will return Proc that looks like this:

proc { |obj, *args, &block| obj.first_name(*args, &block) }

this Proc invokes method specified by original symbol on the object passes as the first parameter and pass all the rest parameters + block as this method arguments.

One more example:

> p = :each.to_proc
=> #<Proc:0x00000001bc28b0>
> p.call([1,2,3]) { |item| puts item+1 }
2
3
4
=> [1, 2, 3]
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
4

It is same with contacts.sort_by {|o| o.first_name}

It returns a Proc object which respond to the given method by sym.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 1
    Downvoted to discourage answers to commonly duplicated questions. – Phrogz Feb 08 '12 at 15:11
  • @Phrogz So should I search first when answer a question? – xdazz Feb 08 '12 at 15:13
  • In my opinion, yes. Sorry. This question should be closed and deleted for the benefit of Stack Overflow. It's harder to get a question deleted when it has answers in it. – Phrogz Feb 08 '12 at 15:30
  • 7
    @xdazz Although this question may be closed, regardless. I dont belive that people should 'search' before answering a question, that definitely seems counterproductive. – Kamilski81 Feb 08 '12 at 15:42
  • @Kamilski81 I appreciate your feedback. I'll go research and discuss more on meta. – Phrogz Feb 08 '12 at 15:50
  • 1
    [meta post](http://meta.stackexchange.com/questions/121656/downvote-answers-to-questions-that-should-be-closed/121657#121657) – Adam Rackis Feb 08 '12 at 16:27
  • 3
    @Kamilski81 - I'm with you - if I see a question I can answer, I answer it. I have better things to do than search for duplicates first. To say nothing of the fact that by the time I'm done searching, multiple answers will already exist on the question – Adam Rackis Feb 08 '12 at 16:27