Last Updated: February 25, 2016
·
1.098K
· 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

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

over 1 year ago ·

@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 ·

Ah, my bad. Thanks for clearing that up.

over 1 year ago ·

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

over 1 year ago ·