From: "esad (Esad Hajdarevic) via ruby-core" Date: 2024-04-23T06:21:46+00:00 Subject: [ruby-core:117650] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly Issue #20444 has been updated by esad (Esad Hajdarevic). nobu (Nobuyoshi Nakada) wrote in #note-1: > `StopIteration.new(3)` does not set `result`, and no way to set it in Ruby level. > > ``` > $ ruby -e 'e = StopIteration.new(3); p e.message, e.result' > "3" > nil > ``` Thanks for the hint. It seems that subclassing StopIteration to provide result works: ``` class MyException < StopIteration def result = 5 end loop { raise MyException } # => 5 ``` ---------------------------------------- Bug #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly https://bugs.ruby-lang.org/issues/20444#change-108060 * Author: esad (Esad Hajdarevic) * Status: Closed * ruby -v: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin20] * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- There was a https://bugs.ruby-lang.org/issues/11498 a while ago which was merged in, but I was surprised to find out that raising `StopIteration` in a loop like `loop { raise StopIteration.new(3) }` returns nil and not 3. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/