I need to allow blocks to be defined and called within the class using instance_exec(via Rails 2.3.2). However, some of these blocks should return early in some situations, which causes me a problem.
My application was created using ruby 1.8.6, but I need to run it on 1.8.7. It seems that between the two versions the ability to return from lambda has been removed. The following works in 1.8.6, but throws a LocalJumpError(unexpected return) in 1.8.7:
class Foo
def square(n)
n ** 2
end
def cube(n)
n ** 3
end
def call_block(*args, &block)
instance_exec *args, &block
end
end
block = lambda { |n|
return square(n) if n < 5
cube(n)
}
f = Foo.new
f.call_block(5, &block)
f.call_block(3, &block)
I decided that I could make it work in 1.8.7 if I replaced returnin my block with next, but it next square(n) if n < 5leads to nilin 1.8.6.
1.8.6, 1.8.7? , , , , .
, , , ruby 1.9?
: , , 1.8.6, 1.8.7, , 1.8.7 instance_exec C, 1.8.6 Rails. instance_exec 1.8.7 Rails, .