ttltvw
Last Updated: February 25, 2016
·
936
· sheerun

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

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

over 1 year ago ·
1828

@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

Ah, my bad. Thanks for clearing that up.

over 1 year ago ·
17654

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

over 1 year ago ·