What happened when passing a method to an iterator method

As you know, wo can pass a method to an iterator method with the &: prefix.
For instance:

 ["a", "b"].map(&:upcase) #=> ["A", "B"] def rettwo 2 end ["a", "b"].map(&:rettwo) #=> [2, 2] 

Here is the problem, when I write a method, pass it a method with the &: prefix, I received an error message: "ArgumentError: no recipient specified."
Let me show the code:

 def a_simple_method &proc puts proc.class # it shows `Proc` proc.call end def a_iterator_method puts yield end a_simple_method &:rettwo #=> ArgumentError: no receiver given a_iterator_method &:rettwo #=> ArgumentError: no receiver given 

What I miss, How the map method as an Array array processes it

+3
source share
1 answer

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) # => "aa" a_iterator_method(&:rettwo) # => "bb" 

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) # => "hello ruby" 

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 
+4
source

All Articles