Gary Wright
2/15/2007 2:50:00 PM
On Feb 15, 2007, at 6:52 AM, Pedro Del Gallego wrote:
> Why the first and the second calls dosnt works and the third works?
> 1.1) loopN 5, {puts "hola"} # compile error , parse error unexpected
> "{", expected "$"
Ruby doesn't expect a comma between the last standard argument and
the optional block argument. Drop the comma.
> 1.2) loopN 5 {puts "hola"} # compile error , unexpected tString.
The {} of the block is binding to the number 5, which is a syntax error.
You need to add parens to clarify your intent as in:
> 1.2) loopN (5) {puts "hola"} # works
This would be more commonly written as:
loopN(5) { puts "hola"}
Alternatively you can use do/end, which has low precedence
loopN 5 do
puts "hola"
end
Gary Wright