1

What I know:

I can call a method with object as parameter in ruby like this:

["1", "2", "3"].map(&method(:Integer))
#=> [1, 2, 3]

&method (basically &) tells its a proc and not an object. Based on it I can define and call my own method too. Like:

def res_str(val)
  puts val
end

["1", "2", "3"].each(&method(:res_str))
# 1
# 2
# 3
#=> ["1", "2", "3"]

Now what if my method takes more than one argument?

def res_str(val, str)
  puts "#{val} - #{str}"
end

My Questions:

  • How can I pass more than one argument in the method using this technique?
  • What is this technique called?

PS: I tried to frame my questions as per my understanding. If I am fundamentally wrong at it, please care to explain.

shivam
  • 16,048
  • 3
  • 56
  • 71
  • 1
    "its a proc and not an object" -- This is contradictory. A proc is an object. – sawa Jan 12 '15 at 07:22
  • 1
    Have a look at @Uri's answer [here](http://stackoverflow.com/questions/23695653/can-you-supply-arguments-to-the-mapmethod-syntax-in-ruby/23711606#23711606). – Cary Swoveland Jan 12 '15 at 07:22
  • @CarySwoveland that completely answers my 1st question. Thanks – shivam Jan 12 '15 at 07:29
  • @sawa I was still trying to understand your answer. I didnot commented yet, as I was going through some basic concepts to understand it better so I can ask question (or accept) accordingly. I didnot hated it by any mean – shivam Jan 12 '15 at 07:32

0 Answers0