From: knu@... Date: 2015-09-01T09:11:30+00:00 Subject: [ruby-core:70644] [Ruby trunk - Feature #11498] [Open] Kernel#loop: return the "result" value of StopIteration Issue #11498 has been reported by Akinori MUSHA. ---------------------------------------- Feature #11498: Kernel#loop: return the "result" value of StopIteration https://bugs.ruby-lang.org/issues/11498 * Author: Akinori MUSHA * Status: Open * Priority: Normal * Assignee: Yukihiro Matsumoto ---------------------------------------- Kernel#loop wraps use of Enumerator rescuing StopIteration raised by Enumerator#next, but it does not provide access to the "result" value of a finished enumerator. If you wanted to get it, you'd have to rescue StopIteration by yourself. ~~~ result = loop { begin puts enum.next rescue StopIteration => exc break exc.result end } ~~~ This feels awkward when loop is capable of rescuing the exception. So, my proposal here is to make Kernel#loop return the "result" value, which currently returns nothing (nil). Kernel#loop takes a "return" of an enumerator (StopIteration) as "break" of a loop, so it should be natural to be able to carry a returned value through to the caller like it was given with "break". With this enhancement, you could write something like this: ~~~ enum = Enumerator.new { |y| y << 1 y << 2 :ok } result = loop { puts enum.next } #=> :ok ~~~ Attached is my implementation. ---Files-------------------------------- loop-result.patch (3.03 KB) -- https://bugs.ruby-lang.org/