# Unexpected return (LocalJumpError)

**URL:** <https://www.ruby-forum.com/t/unexpected-return-localjumperror/153094>\
**Category:** Ruby\
**Created:** [November 12, 2008, 5:40am UTC](https://www.ruby-forum.com/t/unexpected-return-localjumperror/153094 "2008-11-12T05:40:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [November 12, 2008, 5:40am UTC](https://www.ruby-forum.com/t/unexpected-return-localjumperror/153094/1 "2008-11-12T05:40:32Z")

</div>

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

a # =\> It’s OK

def a(blk)  
blk.call  
end

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

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

############

# Help Me^^

############

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [November 12, 2008, 7:26am UTC](https://www.ruby-forum.com/t/unexpected-return-localjumperror/153094/2 "2008-11-12T07:26:30Z")

</div>

Kyung won Cheon wrote:

> def a  
> blk = Proc.new { return }  
> blk.call  
> end
> 
> a # =\> It’s OK
> 
> def a(blk)  
> 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)  
blk.call  
end

def main  
blk = Proc.new { return }  
p “before”  
a blk  
p “after” # never gets here  
end

main # =\> “before”

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [November 12, 2008, 9:55am UTC](https://www.ruby-forum.com/t/unexpected-return-localjumperror/153094/3 "2008-11-12T09:55:07Z")

</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)

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [November 12, 2008, 11:10am UTC](https://www.ruby-forum.com/t/unexpected-return-localjumperror/153094/4 "2008-11-12T11:10:14Z")

</div>

On Wed, Nov 12, 2008 at 9:52 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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)  
> 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  
> b blk  
> puts :in\_a  
> end

def b blk  
c blk  
puts :in\_b  
end

def c blk  
blk.call  
puts :in\_c  
end

def main

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

–

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

–  
C’est véritablement utile puisque c’est joli.

Antoine de Saint Exupéry

---

<div class="post-metadata">

**Author:** ![Kyung\_won\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/47e85d/32.png) [@Kyung\_won\_C](https://www.ruby-forum.com/u/Kyung_won_C)\
**Post date:** [November 12, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/unexpected-return-localjumperror/153094/5 "2008-11-12T18:15:17Z")

</div>

Robert D. 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’  
returns from that method. If the method has already terminated, then  
‘return’ makes no sense and causes an error.

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

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