how does Array#map have parameter to do something like this?

后端 未结 2 1297
梦毁少年i
梦毁少年i 2020-12-04 00:36
def double(a)
 a*2
end

method_object = method(:double)

and here\'s my question, how does this code:

[1,3,5,6].map(&method_obje         


        
相关标签:
2条回答
  • 2020-12-04 01:15

    you can extend class integer:

    class Integer
      def double
        self*2
      end
    end
    
    [1,2,3,4].map(&:double)
    [
        [0] 2,
        [1] 4,
        [2] 6,
        [3] 8
    ]
    
    0 讨论(0)
  • 2020-12-04 01:31

    Simply put, the ampersand & is used to "pack / unpack" a method object to a block, so the effect is more-less the same as if you passed the block.

    You can "get" the block that has been passed to your method by:

    def method_with_block(&foo)
      # do something here
      foo.call
      # do something else
    end
    

    This will be similar to calling yield and not declaring &foo as parameter. I think binding might differ between the two approaches, but in most cases the effect is what you would expect (if I am mistaken, please correct).

    Of course, the ampersand works the other way around - if a method expects a block and you have a proc object, you can simply prepend it with &, just as you wrote. To be more precise, & calls to_proc method of the passed object (Rails uses this in a manner similar to described in this entry about to_proc.

    I hope that this answers some of your doubts. In case there is a mistake in what I wrote, feel free to correct it.

    Readings you might find useful:

    • Programming Ruby, essential book
    • Wikibooks contains a section about this topic
    • This simple blog entry shows an example
    0 讨论(0)
提交回复
热议问题