Hi guys. I have a little question about how ruby interprets de ensure clause in the code. As i know, ensure clause must always be enclosed by begin and end tokens.
For example, if i write this piece of code a kENSURE error will be raised (C:/Documents and Settings/jlomonaco/Desktop/borrar.rb:3: syntax error, unexpecte d kENSURE, expecting $end)
x=5
raise "ERROR!!!" if x==5
ensure
puts "some code has been executed"
However if I wrap this code between begin and end clauses no error will be raised:
begin
x=5
raise "ERROR!!!" if x==5
ensure
puts "some code has been executed"
end
So here comes my question:
I have discovered that inside a function definition isn't obligatory to wrap the ensure token between begin and end; for example i could just write this code and no errors will be raised:
def stackoverflow
x=5
raise "ERROR!!!" if x==5
ensure
puts "some code has been executed"
end
stackoverflow
And if i write the begin and end tokens the code works well too. So where is it obligatory to put the BEGIN and END clauses?
Thanks!