ttltvw
Last Updated: February 25, 2016
·
903
· sheerun
Photo on 08.01.2013 at 04.15

Map with class' method

class Human
  def initialize
    [1, 2, 3].map(&method(:random))
  end

  def random(seed)
    42
  end
end

4 Responses
Add your response

1825
Richhealeyavatar

Pretty sure the & is superfluous, #method should return a callable :)

over 1 year ago ·
1828
Photo on 08.01.2013 at 04.15

@richoh Nope. method returns Method class, and we need to pass block. afaik ampersand operator is the only way to convert proc / lambda / method to block.

over 1 year ago ·
1838
Richhealeyavatar

Ah, my bad. Thanks for clearing that up.

over 1 year ago ·
17654
Default profile 3 normal

For the unenlightened...like me :-. What is the underlying intent in the above example?
Any hints appreciated.

over 1 year ago ·