0

consider the following ruby snippet

def foo
  Proc.new {return 1}
end

def bar
  foo.call
end

def baz
  a = Proc.new {return 1}
  a.call
end

foo.call # error : unexpected return
bar # error : unexpected return
baz # 1

In the above foo.call throws error because its call in main outside of a function

but why does call to bar fail?

the proc should be called inside a function and the return should be working...

  • 1
    See the docs for [Lambda and non-lambda semantics](https://docs.ruby-lang.org/en/master/Proc.html#class-Proc-label-Lambda+and+non-lambda+semantics) regarding `return` within procs. – Stefan Aug 03 '23 at 08:53

0 Answers0