[ruby-core:123181] [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 00:10:38 UTC
List:
ruby-core #123181
Issue #21563 has been updated by jeremyevans0 (Jeremy Evans).
If we want to change the behavior in this case, we should report both the actual argument and the return value of `to_proc`. That's what we do for `*a` where `a.to_a` returns non-Array and for `**h` where `h.to_hash` returns non-Hash):
```ruby
A = Class.new
a = A.new
def a.to_a = 1
def a.to_hash = 1
p(*a) # can't convert A to Array (A#to_a gives Integer) (TypeError)
p(**a) # can't convert A to Hash (A#to_hash gives Integer) (TypeError)
def a.to_proc = 1
p(&a)
# currently: wrong argument type A (expected Proc) (TypeError)
# preferable: can't convert A to Proc (A#to_proc gives Integer) (TypeError)
```
----------------------------------------
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-114512
* 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 -- 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/lists/ruby-core.ml.ruby-lang.org/