"return" from before_save

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?

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
[email protected]

On Jan 30, 3:40 pm, Ralph S. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs