You can not. You can return from methods and exit blocks or loops, but not eval.
Instead, you can try throw / catch block
eval " should_stop = true catch :stop do puts 1 puts 2 throw :stop if should_stop puts 3 end "
or that:
should_stop = true catch :stop do eval " puts 1 puts 2 throw :stop if should_stop puts 3 " end
or just make a conditional expression as Mchl said, since you probably want it to be a conditional stop, not just always, but a catch throw will let you jump out of the block no matter how many levels you need, which makes it more reliable if you need to break out of a nested loop or something like that.
source share