Project

General

Profile

Actions

Bug #21563

closed

Misleading error message when `to_proc` does not return a Proc in an Object used as a &block argument

Added by soulcutter (Bradley Schaefer) 4 days ago. Updated 3 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
[ruby-core:123180]

Description

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.

Actions

Also available in: Atom PDF

Like0
Like2Like0Like0