From: firelzrd@gmail.com
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:51041] [Ruby master Bug#17764] ブロック付きメソッドにおけるproc(または Proc.new)の参照がArgumentErrorを吐く問題
Date: Wed, 31 Mar 2021 03:13:09 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-91198.20210331031308.51332@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-17764.20210331004153.51332@ruby-lang.org>
Issue #17764 has been updated by firelzrd (Masahito Suzuki).
ご教示大変ありがとうございます。
希望した動作ができました。
恥ずかしながら、ブロック引数の指定がない場合にnilとなる挙動を把握できていませんでした。勉強いたします。
諸兄のご親切に重ねて御礼申し上げます。
貴重なお時間をありがとうございました。
osyo (manga osyo) wrote in #note-5:
> やりたいこととしてはこういうことでしょうか?
>
> ```ruby
> def foo(&block)
> # ブロック引数がない場合は block は nil になる
> puts (block ? block : proc{"ブロックなし"}).call
> end
>
> foo #ブロックなし => "ブロックなし"
> foo{"ブロックあり"} # ブロックあり => "ブロックあり"
> ```
matz (Yukihiro Matsumoto) wrote in #note-6:
> 「メソッド呼び出し時のブロック引数は省略可能にしておきたい」という要求の正確な意味がわからなかったのですが、これが「引数リストに&blk変数を指定したくない」というものだとすれば、それは3.0からできなくなりました。これまではprocメソッドを引数なしで呼び出す方法でメソッドに渡されたブロックをProcとして取り出せていましたが、暗黙のコンテキスト参照が入りますし、読解の点からも望ましくないと考えての変更です。「&blk引数」を使ってください。それに、たぶんこっちのほうが高速です。
>
> Matz.
----------------------------------------
Bug #17764: ブロック付きメソッドにおけるproc(または Proc.new)の参照がArgumentErrorを吐く問題
https://bugs.ruby-lang.org/issues/17764#change-91198
* Author: firelzrd (Masahito Suzuki)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
# ブロック付きメソッドにおけるproc(または Proc.new)の参照がArgumentErrorを吐く問題
# Ruby 3.0.0 linux x86-64およびWindows上のmingw-x86-64バイナリにて発生し、2.7.2-1ではいずれも発生しないことを確認済み。
# また、2021/3/31現在、TryRubyサイト( https://try.ruby-lang.org/ ) の上でも発生することを確認済み。
def foo
puts (block_given? ? proc : proc{"ブロックなし"}).call
end
# @ruby 3.0.0
foo #ブロックなし => "ブロックなし"
foo{"ブロックあり"} #ブロックあり => ArgumentError: tried to create Proc object without a block
# @ruby 2.7.2
foo #ブロックなし => "ブロックなし"
foo{"ブロックあり"} #ブロックあり => "ブロックあり"
---Files--------------------------------
argumenterror.png (125 KB)
--
https://bugs.ruby-lang.org/
prev parent reply other threads:[~2021-03-31 3:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-31 0:41 [ruby-dev:51033] " firelzrd
2021-03-31 0:56 ` [ruby-dev:51034] " manga.osyo
2021-03-31 2:30 ` [ruby-dev:51036] " firelzrd
2021-03-31 2:35 ` [ruby-dev:51037] " ko1
2021-03-31 2:57 ` [ruby-dev:51038] " firelzrd
2021-03-31 3:04 ` [ruby-dev:51039] " manga.osyo
2021-03-31 3:05 ` [ruby-dev:51040] " matz
2021-03-31 3:13 ` firelzrd [this message]
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-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-91198.20210331031308.51332@ruby-lang.org \
--to=firelzrd@gmail.com \
--cc=ruby-dev@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 NNTP newsgroup(s).