1

Possible Duplicate:
What do you call the &: operator in Ruby?

I see '.map(&:chomp)' all the time

I know what chomp and map do, but I want to know what &: does and I'd like to know why I can't find it on the web after 30 minutes of googling.....

Community
  • 1
  • 1
cdnbcguy
  • 109
  • 1
  • 3
  • 11
  • This is the 22nd duplicate of this question. When are the StackOverflow developers finally going to fix the broken search function so that this doesn't happen anymore? – Jörg W Mittag Jan 10 '13 at 09:26

1 Answers1

5

It's Symbol#to_proc, and it turns the symbol into a proc which attempts to invoke the given method on its argument, returning the result.

x = :reverse.to_proc

x.call("asdf") # "fdsa", like calling "asdf".reverse

In your case, .map(&:chomp) is equivalent to .map { |x| x.chomp }.

If you can't find it by Googling, it's because you're Googling the wrong thing. It's a well-known Ruby idiom.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    If they [search for `to_proc`](https://www.google.com/search?client=safari&rls=en&q=ruby+to_proc&ie=UTF-8&oe=UTF-8) Google spits out all sorts of info. – the Tin Man Jan 10 '13 at 03:29