Project

General

Profile

Feature #11498

Kernel#loop: return the "result" value of StopIteration

Added by knu (Akinori MUSHA) about 4 years ago. Updated almost 4 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:<unknown>]

Description

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) loop-result.patch knu (Akinori MUSHA), 09/01/2015 09:09 AM

Associated revisions

Revision f4c48717
Added by knu (Akinori MUSHA) almost 4 years ago

Kernel#loop returns the result value of a finished iterator

  • vm_eval.c (rb_f_loop): When a loop is stopped by a StopIteration exception, return what the enumerator has returned instead of nil. [ruby-core:71133] [Feature #11498]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52218 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 52218
Added by knu (Akinori MUSHA) almost 4 years ago

Kernel#loop returns the result value of a finished iterator

  • vm_eval.c (rb_f_loop): When a loop is stopped by a StopIteration exception, return what the enumerator has returned instead of nil. [ruby-core:71133] [Feature #11498]

Revision 52218
Added by knu (Akinori MUSHA) almost 4 years ago

Kernel#loop returns the result value of a finished iterator

  • vm_eval.c (rb_f_loop): When a loop is stopped by a StopIteration exception, return what the enumerator has returned instead of nil. [ruby-core:71133] [Feature #11498]

Revision 52218
Added by knu (Akinori MUSHA) almost 4 years ago

Kernel#loop returns the result value of a finished iterator

  • vm_eval.c (rb_f_loop): When a loop is stopped by a StopIteration exception, return what the enumerator has returned instead of nil. [ruby-core:71133] [Feature #11498]

Revision 52218
Added by knu (Akinori MUSHA) almost 4 years ago

Kernel#loop returns the result value of a finished iterator

  • vm_eval.c (rb_f_loop): When a loop is stopped by a StopIteration exception, return what the enumerator has returned instead of nil. [ruby-core:71133] [Feature #11498]

Revision 52218
Added by knu (Akinori MUSHA) almost 4 years ago

Kernel#loop returns the result value of a finished iterator

  • vm_eval.c (rb_f_loop): When a loop is stopped by a StopIteration exception, return what the enumerator has returned instead of nil. [ruby-core:71133] [Feature #11498]

History

Updated by matz (Yukihiro Matsumoto) almost 4 years ago

I like the idea. If anything bad happens by the patch, I'd love to accept this.

Matz.

#2

Updated by knu (Akinori MUSHA) almost 4 years ago

  • Status changed from Open to Closed

Applied in changeset r52218.


Kernel#loop returns the result value of a finished iterator

  • vm_eval.c (rb_f_loop): When a loop is stopped by a StopIteration exception, return what the enumerator has returned instead of nil. [ruby-core:71133] [Feature #11498]

Also available in: Atom PDF