Here is what works. Explanation below.
class String def rettwo self + self end end def a_simple_method &proc proc.call('a') end def a_iterator_method yield 'b' end a_simple_method(&:rettwo)
The &: construct is called Symbol#to_proc . It turns the character into proc. This proc expects a receiver as the first argument. The remaining arguments are used to call proc. You are not passing any arguments, so the "recipient is not specified" error.
Additional arguments are shown here:
class String def say name "#{self} #{name}" end end def a_simple_method &proc proc.call('hello', 'ruby') end a_simple_method(&:say)
Here is the definition of the # to_proc character from some blog post since 2008. The modern # to_proc character seems to be implemented in C, but it still helps to understand.
class Symbol def to_proc Proc.new { |*args| args.shift.__send__(self, *args) } end end
source share