# Unexpected return (LocalJumpError)

**URL:** <https://rubytalk.org/t/unexpected-return-localjumperror/50139>\
**Category:** ruby-talk\
**Created:** [12 November 2008 04:37 UTC](https://rubytalk.org/t/unexpected-return-localjumperror/50139 "2008-11-12T04:37:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyung\_won\_Cheon1](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@Kyung\_won\_Cheon1](https://rubytalk.org/u/Kyung_won_Cheon1)\
**Post date:** [12 November 2008 04:37 UTC](https://rubytalk.org/t/unexpected-return-localjumperror/50139/1 "2008-11-12T04:37:52Z")

</div>

def a  
&nbsp;&nbsp;blk = Proc.new { return }  
&nbsp;&nbsp;blk.call  
end

a # =\> It's OK

def a(blk)  
&nbsp;&nbsp;blk.call  
end

blk = lambda { return }  
a blk # =\> It's OK

blk = Proc.new { return }  
a blk # =\> unexpected return (LocalJumpError)

> **···**
>
> ############  
> # Help Me^^  
> ############  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_Gold](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Mike\_Gold](https://rubytalk.org/u/Mike_Gold)\
**Post date:** [12 November 2008 06:23 UTC](https://rubytalk.org/t/unexpected-return-localjumperror/50139/2 "2008-11-12T06:23:49Z")

</div>

Kyung won Cheon wrote:

> def a  
> &nbsp;&nbsp;blk = Proc.new { return }  
> &nbsp;&nbsp;blk.call  
> end
> 
> a # =\> It's OK
> 
> def a(blk)  
> &nbsp;&nbsp;blk.call  
> end
> 
> blk = lambda { return }  
> a blk # =\> It's OK
> 
> blk = Proc.new { return }  
> a blk # =\> unexpected return (LocalJumpError)

% ruby -e 'return'  
-e:1: unexpected return (LocalJumpError)

That is what you are seeing. 'return' interrupts a method, but there is  
no method at the top-level scope.

def a(blk)  
&nbsp;&nbsp;blk.call  
end

def main  
&nbsp;&nbsp;blk = Proc.new { return }  
&nbsp;&nbsp;p "before"  
&nbsp;&nbsp;a blk  
&nbsp;&nbsp;p "after" # never gets here  
end

main # =\> "before"

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [12 November 2008 08:52 UTC](https://rubytalk.org/t/unexpected-return-localjumperror/50139/3 "2008-11-12T08:52:26Z")

</div>

For the differences between lambda and Proc, including a lot of gory  
detail, see  
[http://innig.net/software/ruby/closures-in-ruby.rb](http://innig.net/software/ruby/closures-in-ruby.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [12 November 2008 10:06 UTC](https://rubytalk.org/t/unexpected-return-localjumperror/50139/4 "2008-11-12T10:06:55Z")

</div>

or better dont,  
All you need to know is that lambda and Proc::new behave as they are  
designed to behave, therefore the behavior you encountered is not a  
bug but a feature as surprising as that comes to most of us ;). IIRC  
this will change in 1.9  
To better understand what really happens just run this code:  
def a blk  
&nbsp;&nbsp;b blk  
&nbsp;&nbsp;puts :in\_a  
end

def b blk  
&nbsp;&nbsp;c blk  
&nbsp;&nbsp;puts :in\_b  
end

def c blk  
&nbsp;&nbsp;blk.call  
&nbsp;&nbsp;puts :in\_c  
end

def main

&nbsp;&nbsp;#a lambda{ puts :in\_lambda; return; puts :nonsense}  
&nbsp;&nbsp;#a Proc::new{ puts :in\_proc; return; puts :nonsense}  
&nbsp;&nbsp;puts :in\_main  
end

> **···**
>
> On Wed, Nov 12, 2008 at 9:52 AM, Brian Candler \<b.candler@pobox.com\> wrote:
> 
> > For the differences between lambda and Proc, including a lot of gory  
> > detail, see  
> > [http://innig.net/software/ruby/closures-in-ruby.rb](http://innig.net/software/ruby/closures-in-ruby.rb)
> 
> --
> 
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> C'est véritablement utile puisque c'est joli.
> 
> Antoine de Saint Exupéry

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [12 November 2008 17:12 UTC](https://rubytalk.org/t/unexpected-return-localjumperror/50139/5 "2008-11-12T17:12:32Z")

</div>

Robert Dober wrote:

> IIRC this will change in 1.9

In what way?

proc is now an alias for Proc.new instead of lambda. But what other  
changes are in the pipeline?

BTW I agree that this is a feature, not a bug. But it's not obvious to  
the newcomer that there are two, rather different, closure-like objects:

- lambda is standalone. A 'return' returns from the lambda.

- Proc.new / block is created within the context of a method. A 'return'  
&nbsp;&nbsp;returns from that method. If the method has already terminated, then  
&nbsp;&nbsp;'return' makes no sense and causes an error.

The first is what Lisp programmers expect. The second is what makes it  
possible to write

&nbsp;&nbsp;&nbsp;def calculate\_something(foo)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo.each do |params|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return val if cond  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
