class Test
def foo
throw(:label, foo)
"should never get here"
end
def bar
"bar"
end
end
test = Test.new
Now I tried the below:
puts("bar -> " + catch(:label) {test.bar})
and got :
bar -> bar
=> nil
Now when I tried:
puts("foo -> " + catch(:label) {test.foo})
I expected that i would get nil but actually got the below:
SystemStackError: stack level too deep
from /usr/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!
I am unable to explain myself why this. Could anyone help me for the same?
The infinite loop happens outside the throw/catch.
The value returned has to be generated first, no lazy eval there. So
it calls foo again, and you have your infinite recursion without any
stopping point. If you want nil, use