`&:views_count` in `Post.published.collect(&:views_count)`
This is actually a rather clever hack made it into ruby 1.9.
Basically, &
in front of a variable in ruby coerces it into a proc. It does that by calling to_proc
. Some clever fellow (first time I saw this was in _whys code, but I won't credit him cause I don't know if he came up with it) added a to_proc
method to Symbol
, that is essentially {|obj| obj.send self}
.
There aren't many coercians in ruby, but it seems like all of them are mostly used to do hacks like this (like !! to coerce any type into a boolean)
what is the functionality of &: operator in ruby?
There isn't a &:
operator in Ruby. What you are seeing is the &
operator applied to a :symbol
.
In a method argument list, the &
operator takes its operand, converts it to a Proc
object if it isn't already (by calling to_proc
on it) and passes it to the method as if a block had been used.
my_proc = Proc.new { puts "foo" }
my_method_call(&my_proc) # is identical to:
my_method_call { puts "foo" }
So the question now becomes "What does Symbol#to_proc
do?", and that's easy to see in the Rails documentation:
Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:
# The same as people.collect { |p| p.name }
people.collect(&:name)
# The same as people.select { |p| p.manager? }.collect { |p| p.salary }
people.select(&:manager?).collect(&:salary)
What does temps.each(&:valid?) mean in Ruby?
The &
is called the to_proc
operator. It expands the symbol (:valid?
) into a Proc.
So your example is equivalent to:
temps.each { |t| t.valid? }
what does &: mean in ruby, is it a block mixed with a symbol?
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]
Related Topics
Actioncontroller::Unknownformat
Why Are Gems Installed in a Directory With a Different Ruby Version Than I'M Running
Ruby: Inherit Code That Works With Class Variables
Understanding [Classone, Classtwo].Each(&:My_Method)
How to Avoid Tripping Over Utf-8 Bom When Reading Files
How to Break Out from a Ruby Block
How to Download a File from a Url and Save It in Rails
Difference Between Ruby 1.8 and Ruby 1.9
How to Implement an Abstract Class in Ruby
Using 'Return' in a Ruby Block
Submit Post Data from Controller to Another Website in Rails
Pressing Ctrl + a in Selenium Webdriver
Methods in Ruby: Objects or Not
How to Reload the Current Page in Ruby on Rails
Why Do Ruby Setters Need "Self." Qualification Within the Class
Pg::Connectionbad - Could Not Connect to Server: Connection Refused