# "return" from before\_save

**URL:** <https://www.ruby-forum.com/t/return-from-before-save/182594>\
**Category:** Rails\
**Created:** [January 30, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/return-from-before-save/182594 "2010-01-30T16:40:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 30, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/return-from-before-save/182594/1 "2010-01-30T16:40:18Z")

</div>

I wish to exit a before\_save block. If I invoke

return true

in the before\_save blockk, I get a

return can’t jump across threads

error return.

Hitting the Pickaxe book, it says that returns from a block will cause  
this error. (Why? I don’t know.)

So … how does one exit gracefully from a block prior to falling off  
the end of the block?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 30, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/return-from-before-save/182594/2 "2010-01-30T18:13:38Z")

</div>

Ralph S. wrote:

> I wish to exit a before\_save block. If I invoke
> 
> return true
> 
> in the before\_save blockk, I get a
> 
> return can’t jump across threads
> 
> error return.
> 
> Hitting the Pickaxe book, it says that returns from a block will cause  
> this error. (Why? I don’t know.

Nor should you care.

> So … how does one exit gracefully from a block prior to falling off  
> the end of the block?

## One doesn’t AFAIK. In the general case, you could use a conditional statement or raise an exception. In your case, I’d advise using a method callback instead of a block. Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 30, 2010, 9:43pm UTC](https://www.ruby-forum.com/t/return-from-before-save/182594/3 "2010-01-30T21:43:16Z")

</div>

On Jan 30, 3:40 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hitting the Pickaxe book, it says that returns from a block will cause  
> this error. (Why? I don’t know.)
> 
> So … how does one exit gracefully from a block prior to falling off  
> the end of the block?

def foo  
yield  
end

foo { return 1; puts ‘return’} #=\> LocalJumpError: unexpected return

foo { next 1; puts ‘not reached’} #=\> 1

Fred
