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 map(&:name) mean in Ruby?
It's shorthand for tags.map(&:name.to_proc).join(' ')
If foo
is an object with a to_proc
method, then you can pass it to a method as &foo
, which will call foo.to_proc
and use that as the method's block.
The Symbol#to_proc
method was originally added by ActiveSupport but has been integrated into Ruby 1.8.7. This is its implementation:
class Symbol
def to_proc
Proc.new do |obj, *args|
obj.send self, *args
end
end
end
What does the `&` mean in the following ruby syntax?
&:price is shorthand for "use the #price method on every member of the collection".
Unary "&", when passed as an argument into a method tells Ruby "take this and turn it into a Proc". The #to_proc method on a symbol will #send that symbol to the receiving object, which invokes the corresponding method by that name.
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]
What exactly is `&:capitalize` in Ruby?
foo(&a_proc_object)
turns a_proc_object
into a block and calls foo with that block.
foo(¬_a_proc_object)
calls to_proc
on not_a_proc_object
and then turns the proc object returned by to_proc
into a block and calls foo with that block.
In ruby 1.8.7+ and active support Symbol#to_proc
is defined to return a proc which calls the method named by the symbol on the argument to the proc.
Understanding [ClassOne, ClassTwo].each(&:my_method)
This relies upon a Ruby 1.9 extension that can be done in 1.8 by including the following:
class Symbol
def to_proc
proc { |obj, *args| obj.send(self, *args) }
end
end
I believe Rails defines this in ActiveSupport
.
What does this ampersand mean?
It's basically shorthand for this:
[Category, Product, Person].each { |e| e.delete_all }
That is, it sends delete_all
to each element of the iterator.
What is this &:last Ruby construct called?
This is shorthand for:
survey.map { |s| s.questions }.flatten.compact
It's the Symbol#to_proc
method. It used to be a part of Rails' ActiveSupport, but has since been added to Ruby syntax.
As far as performance goes, I wrote a quick benchmark script to get an idea of performance effect in both 1.8 and 1.9.
require 'benchmark'
many = 500
a = (1..10000).to_a
Benchmark.bm do |x|
x.report('block once') { a.map { |n| n.to_s } }
x.report('to_proc once') { a.map(&:to_s) }
x.report('block many') { many.times { a.map { |n| n.to_s } } }
x.report('to_proc many') { many.times { a.map(&:to_s) } }
end
First off, before giving you the results - if you weren't already sure that Ruby 1.9 was a huge speed improvement in general, prepare to be blown away.
Ruby 1.8 results:
user system total real
block once 0.020000 0.000000 0.020000 ( 0.016781)
to_proc once 0.010000 0.000000 0.010000 ( 0.013881)
block many 6.680000 1.100000 7.780000 ( 7.780532)
to_proc many 7.370000 0.540000 7.910000 ( 7.902935)
Ruby 1.9 results:
user system total real
block once 0.010000 0.000000 0.010000 ( 0.011433)
to_proc once 0.000000 0.000000 0.000000 ( 0.004929)
block many 4.060000 0.000000 4.060000 ( 4.057013)
to_proc many 2.810000 0.000000 2.810000 ( 2.810312)
First off: Wow. Ruby 1.9 is fast. But the more relevant conclusions we draw here are interesting:
- In both cases, for only one run,
to_proc
is clearly faster. In 1.8 on the many-times run, it's tad slower. This seems to indicate that the only real performance bottleneck is creating all those Proc objects. - In Ruby 1.9, however, the
to_proc
method is clearly much faster than blocks, no matter how many times you do it. In this case, you not only get cleaner code, but improved performance, as well.
In the end, no matter which version you're using, to_proc
is clearly not enough of a performance issue to be worth not using - in fact, it sometimes speeds things up!
`&: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)
Related Topics
Why Is This Not a Syntax Error
Difference Between Lambda and -> Operator in Ruby
Ruby on Rails - Link_To Button/Css
How to Serialize an Object Using Tcpserver Inside
How to Set a Variable from a Helper Method for Inclusion in a SASS SCSS Stylesheet
Ruby on Rails - Doesn't Create Script/Server
Creating a Setter Method That Takes Extra Arguments in Ruby
Nokogiri: Searching for <Div> Using Xpath
Converting a Unique Seed String into a Random, Yet Deterministic, Float Value in Ruby
What Is the Preferred Way of Performing Non Blocking I/O in Ruby