ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "esad (Esad Hajdarevic) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "esad (Esad Hajdarevic)" <noreply@ruby-lang.org>
Subject: [ruby-core:117679] [Ruby master Feature#20444] Kernel#loop: returning the "result" value of StopIteration doesn't work when raised directly
Date: Wed, 24 Apr 2024 11:00:10 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-108089.20240424110010.53307@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-20444.20240422214119.53307@ruby-lang.org

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/

  parent reply	other threads:[~2024-04-24 11:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` esad (Esad Hajdarevic) via ruby-core [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-108089.20240424110010.53307@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    --cc=ruby-core@ml.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).