Project

General

Profile

Feature #3232

Loops (while/until) should return last statement value if any, like if/unless

Added by Eregon (Benoit Daloze) about 10 years ago. Updated about 9 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
[ruby-core:29920]

Description

=begin
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.
=end

Also available in: Atom PDF