[ruby-core:123183] [Ruby Bug#21563] Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
From:
"jeremyevans0 (Jeremy Evans) via ruby-core" <ruby-core@...>
Date:
2025-09-06 06:58:38 UTC
List:
ruby-core #123183
Issue #21563 has been updated by jeremyevans0 (Jeremy Evans).
I submitted a PR to make the error messages for & consistent with */**: https://github.com/ruby/ruby/pull/14463
----------------------------------------
Bug #21563: Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument
https://bugs.ruby-lang.org/issues/21563#change-114514
* Author: soulcutter (Bradley Schaefer)
* Status: Open
* ruby -v: ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
When a class implements `#to_proc` it should always return a Proc. Nonetheless bugs are possible, and when an implementation returns something else, and is used as a block argument to a method, you get an error that claims that the argument type is the implementing class rather than the type that was returned from `to_proc`.
Reproduction
```
class SayHi
def call = "hi"
def to_proc = "obviously not a proc"
end
def callablock(&block) = block.call
callablock &SayHi.new # wrong argument type SayHi (expected Proc) (TypeError)
```
In real-world code it may not be _this_ obvious that `to_proc` is returning the wrong type, and so I would expect the error would report the type of object returned by `to_proc` so that you have a more-direct path to understanding the misbehavior. In this case I would expect:
wrong argument type String (expected Proc)
Or maybe something more-descriptive as an error message, but I don't have a concrete suggestion for that.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- [email protected]
To unsubscribe send an email to [email protected]
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/