From: "esad (Esad Hajdarevic) via ruby-core" Date: 2024-04-24T11:00:10+00:00 Subject: [ruby-core:117679] [Ruby master Feature#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly Issue #20444 has been updated by esad (Esad Hajdarevic). ufuk (Ufuk Kayserilioglu) wrote in #note-7: > @esad If you just want to return a result from the `loop`, you can use `break ` to do that: > ``` > $ ruby -e "puts loop { break 3 }" > 3 Calling break from a block passed to a ractor will raise an exception. I think some sample code about my example will be helpful: Let's make a ractor that just calls all blocks passed to it in a loop: ``` r = Ractor.new do block = Ractor.receive loop { block.call() } end ``` Now let's send it a block that raises StopIteration: ``` block = true.instance_eval { proc { raise StopIteration, 3 } } # instance_eval in true gives us a "shareable" proc r.send Ractor.make_shareable(block) r.take # => nil ``` Let's try with a subclass (this works) ``` class MyStop < ::StopIteration attr_reader :result def initialize(result) @result = result end end block = true.instance_eval { proc { raise MyStop, 3 } } r.send Ractor.make_shareable(block) r.take # => 3 ``` ---------------------------------------- Feature #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly https://bugs.ruby-lang.org/issues/20444#change-108089 * Author: esad (Esad Hajdarevic) * Status: Feedback ---------------------------------------- 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/