Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "temps.each(&:valid?)" mean in Ruby? [duplicate]

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

What does the &:valid? found in the each mean? I've seen .each do |r| or whatever, but not sure how this one works?

like image 433
Joshua F. Rountree Avatar asked Dec 04 '22 17:12

Joshua F. Rountree


2 Answers

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? }
like image 105
simonmenke Avatar answered Dec 22 '22 09:12

simonmenke


&:symbol is a shorthand for symbol to proc.

Here's a good blog post on it. http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand

like image 35
Joe Pym Avatar answered Dec 22 '22 08:12

Joe Pym