What are the pipe symbols for in Ruby?
I\'m learning Ruby and RoR, coming from a PHP and Java background, but I keep coming across code like this:
de
@names.each do |name|
puts "Hello #{name}!"
end
at http://www.ruby-lang.org/en/documentation/quickstart/4/ is accompanied by this explanation:
each
is a method that accepts a block of code then runs that block of code for every element in a list, and the bit betweendo
andend
is just such a block. A block is like an anonymous function orlambda
. The variable between pipe characters is the parameter for this block.What happens here is that for every entry in a list,
name
is bound to that list element, and then the expressionputs "Hello #{name}!"
is run with that name.