From: Robert Klemme Date: 2008-03-07T02:59:55+09:00 Subject: Re: the value in ensure block is not returned On 06.03.2008 16:46, Oliver Peng wrote: > Here is the sample code: > > irb(main):001:0> def test > irb(main):002:1> begin > irb(main):003:2* a = 1 > irb(main):004:2> b = 2 > irb(main):005:2> b > irb(main):006:2> ensure > irb(main):007:2* a > irb(main):008:2> end > irb(main):009:1> end > => nil > irb(main):010:0> test > => 2 > > I am expecting that 1 should be returned by is 2. Why should it? If you think about it you will see that it's actually good the way it is. The result of evaluating code in /ensure/ cannot be returned under all circumstances (see Gary's reply). And for normal execution of a block of code you want the result of the code block to be returned - not the result of /ensure/. The code in /ensure/ is intended to do some cleanup that has to occur under all circumstances but not interfere with normal processing. > So it is obviously that all codes in ensure block have been run. But the > return value is always the last value before ensure block. Yes, no problem here. Cheers robert