Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: unexpected semicolon in block parameters

Tags:

ruby

I just started learning Ruby. I typed the example:

x = 10
5.times do |y; x|
  x = y
  puts "x inside the block: #{x}"
end
puts "x outside the block: #{x}"

And I have an error:

hello.rb:3: syntax error, unexpected ';', expecting '|' 5.times do |y; x|

Explain to me please what does it mean? This code should works, as I understand the chapter.

like image 961
I159 Avatar asked Jan 17 '23 17:01

I159


1 Answers

That's a new 1.9 construct called block local arguments, you're using 1.8.


It also works with lambdas (including stabbed), which is nice:

> x = 42
> love_me = ->(y; x) do
*   x = y
*   puts "x inside the block: #{x}"
* end
> 2.times &love_me
x inside the block: 0
x inside the block: 1
> puts "x outside the block: #{x}"
x outside the block: 42
like image 159
Dave Newton Avatar answered Jan 24 '23 18:01

Dave Newton