ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
@ 2024-04-22 21:41 esad (Esad Hajdarevic) via ruby-core
  2024-04-23  5:02 ` [ruby-core:117649] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: esad (Esad Hajdarevic) via ruby-core @ 2024-04-22 21:41 UTC (permalink / raw)
  To: ruby-core; +Cc: esad (Esad Hajdarevic)

Issue #20444 has been reported by esad (Esad Hajdarevic).

----------------------------------------
Bug #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
https://bugs.ruby-lang.org/issues/20444

* Author: esad (Esad Hajdarevic)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117649] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
@ 2024-04-23  5:02 ` nobu (Nobuyoshi Nakada) via ruby-core
  2024-04-23  6:21 ` [ruby-core:117650] " esad (Esad Hajdarevic) via ruby-core
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-04-23  5:02 UTC (permalink / raw)
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

Issue #20444 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Open to Closed

`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
```

----------------------------------------
Bug #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
https://bugs.ruby-lang.org/issues/20444#change-108059

* 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117650] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
  2024-04-23  5:02 ` [ruby-core:117649] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-04-23  6:21 ` esad (Esad Hajdarevic) via ruby-core
  2024-04-24  2:40 ` [ruby-core:117667] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: esad (Esad Hajdarevic) via ruby-core @ 2024-04-23  6:21 UTC (permalink / raw)
  To: ruby-core; +Cc: esad (Esad Hajdarevic)

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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117667] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
  2024-04-23  5:02 ` [ruby-core:117649] " nobu (Nobuyoshi Nakada) via ruby-core
  2024-04-23  6:21 ` [ruby-core:117650] " esad (Esad Hajdarevic) via ruby-core
@ 2024-04-24  2:40 ` nobu (Nobuyoshi Nakada) via ruby-core
  2024-04-24  9:07 ` [ruby-core:117673] [Ruby master Feature#20444] " esad (Esad Hajdarevic) via ruby-core
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-04-24  2:40 UTC (permalink / raw)
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

Issue #20444 has been updated by nobu (Nobuyoshi Nakada).


I'm curious what your use case is.

Although I don't know the reason why `StopIteration#initialize` does not have the argument for `result`, it would be difficult to change it now because of the backward compatibility.

----------------------------------------
Bug #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
https://bugs.ruby-lang.org/issues/20444#change-108075

* 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117673] [Ruby master Feature#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
                   ` (2 preceding siblings ...)
  2024-04-24  2:40 ` [ruby-core:117667] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-04-24  9:07 ` esad (Esad Hajdarevic) via ruby-core
  2024-04-24 10:40 ` [ruby-core:117678] " ufuk (Ufuk Kayserilioglu) via ruby-core
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: esad (Esad Hajdarevic) via ruby-core @ 2024-04-24  9:07 UTC (permalink / raw)
  To: ruby-core; +Cc: esad (Esad Hajdarevic)

Issue #20444 has been updated by esad (Esad Hajdarevic).


nobu (Nobuyoshi Nakada) wrote in #note-3:
> I'm curious what your use case is.
> 
> Although I don't know the reason why `StopIteration#initialize` does not have the argument for `result`, it would be difficult to change it now because of the backward compatibility.

I think my use case is a bit of an edge case - I am passing a block into a Ractor where it runs in a loop. This way I can control exit from the loop, but obviously this can be also refactored into a normal result value of the block and "manual" looping control depending on the result.


----------------------------------------
Feature #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
https://bugs.ruby-lang.org/issues/20444#change-108084

* 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117678] [Ruby master Feature#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
                   ` (3 preceding siblings ...)
  2024-04-24  9:07 ` [ruby-core:117673] [Ruby master Feature#20444] " esad (Esad Hajdarevic) via ruby-core
@ 2024-04-24 10:40 ` ufuk (Ufuk Kayserilioglu) via ruby-core
  2024-04-24 11:00 ` [ruby-core:117679] " esad (Esad Hajdarevic) via ruby-core
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ufuk (Ufuk Kayserilioglu) via ruby-core @ 2024-04-24 10:40 UTC (permalink / raw)
  To: ruby-core; +Cc: ufuk (Ufuk Kayserilioglu)

Issue #20444 has been updated by ufuk (Ufuk Kayserilioglu).


@esad If you just want to return a result from the `loop`, you can use `break <value>` to do that:
```
$ ruby -e "puts loop { break 3 }"
3
```

You shouldn't have to deal with anything low level like `StopIteration` to do that.

----------------------------------------
Feature #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
https://bugs.ruby-lang.org/issues/20444#change-108088

* 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117679] [Ruby master Feature#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
                   ` (4 preceding siblings ...)
  2024-04-24 10:40 ` [ruby-core:117678] " ufuk (Ufuk Kayserilioglu) via ruby-core
@ 2024-04-24 11:00 ` esad (Esad Hajdarevic) via ruby-core
  2024-04-26  8:28 ` [ruby-core:117722] " nobu (Nobuyoshi Nakada) via ruby-core
  2024-04-26 10:07 ` [ruby-core:117726] " esad (Esad Hajdarevic) via ruby-core
  7 siblings, 0 replies; 9+ messages in thread
From: esad (Esad Hajdarevic) via ruby-core @ 2024-04-24 11:00 UTC (permalink / raw)
  To: ruby-core; +Cc: esad (Esad Hajdarevic)

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 <value>` 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117722] [Ruby master Feature#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
                   ` (5 preceding siblings ...)
  2024-04-24 11:00 ` [ruby-core:117679] " esad (Esad Hajdarevic) via ruby-core
@ 2024-04-26  8:28 ` nobu (Nobuyoshi Nakada) via ruby-core
  2024-04-26 10:07 ` [ruby-core:117726] " esad (Esad Hajdarevic) via ruby-core
  7 siblings, 0 replies; 9+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-04-26  8:28 UTC (permalink / raw)
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

Issue #20444 has been updated by nobu (Nobuyoshi Nakada).


That example does not need `Ractor`.

```ruby
class MyStop < ::StopIteration
  attr_reader :result
  def initialize(result)
    @result = result
  end
end

block = proc {raise StopIteration, 3}
p Thread.start {loop {block.call}}.value #=> nil

block = proc { raise MyStop, 3 }
p Thread.start {loop {block.call}}.value #=> 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-108130

* 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:117726] [Ruby master Feature#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
  2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
                   ` (6 preceding siblings ...)
  2024-04-26  8:28 ` [ruby-core:117722] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-04-26 10:07 ` esad (Esad Hajdarevic) via ruby-core
  7 siblings, 0 replies; 9+ messages in thread
From: esad (Esad Hajdarevic) via ruby-core @ 2024-04-26 10:07 UTC (permalink / raw)
  To: ruby-core; +Cc: esad (Esad Hajdarevic)

Issue #20444 has been updated by esad (Esad Hajdarevic).


nobu (Nobuyoshi Nakada) wrote in #note-9:
> That example does not need `Ractor`.

Yes, you are right, it actually doesn't need Thread either, and is simply about calling a block in a loop and how to break the loop from the called block:

`proc { break 3}.then { |p| loop { p.call() }}` raises exception, and `proc { raise StopIteration, 3 }.then { |p| loop { p.call() }}` returns `nil`, etc.


----------------------------------------
Feature #20444: Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
https://bugs.ruby-lang.org/issues/20444#change-108134

* 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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-04-26 10:07 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-22 21:41 [ruby-core:117646] [Ruby master Bug#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly esad (Esad Hajdarevic) via ruby-core
2024-04-23  5:02 ` [ruby-core:117649] " nobu (Nobuyoshi Nakada) via ruby-core
2024-04-23  6:21 ` [ruby-core:117650] " esad (Esad Hajdarevic) via ruby-core
2024-04-24  2:40 ` [ruby-core:117667] " nobu (Nobuyoshi Nakada) via ruby-core
2024-04-24  9:07 ` [ruby-core:117673] [Ruby master Feature#20444] " esad (Esad Hajdarevic) via ruby-core
2024-04-24 10:40 ` [ruby-core:117678] " ufuk (Ufuk Kayserilioglu) via ruby-core
2024-04-24 11:00 ` [ruby-core:117679] " esad (Esad Hajdarevic) via ruby-core
2024-04-26  8:28 ` [ruby-core:117722] " nobu (Nobuyoshi Nakada) via ruby-core
2024-04-26 10:07 ` [ruby-core:117726] " esad (Esad Hajdarevic) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).