Feature #3232: Loops (while/until) should return last statement value if any, like if/unless
http://redmine.ruby-lang.org/issues/show/3232

Author: Benoit Daloze
Status: Open, Priority: Normal
Category: core, Target version: 1.9.x

In conditions:
if true
  2
end #=> 2

In loops:
i = 0
while i < 3
  i += 1
  "return value"
end #=> nil

I suggest the return value of loops to be the return value of
the last "block" evaluated between "while/until" and "end".

It would be nil if the condition is not met from the first evaluation.

Is this reasonable to implement ?

It would come very handy in situations like:
while condition
  ...
  my_method_return_value
end
my_method_return_value

because you could remove the last line.

The idea came up on ruby-talk: http://www.ruby-forum.com/topic/208978

Any thoughts?

Regards,
B.D.


----------------------------------------
http://redmine.ruby-lang.org