Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a method as a parameter

Tags:

ruby

Is there are way to pass a method as a parameter without using a Proc object or block? The answers I've seen to similar questions rely on creating a Proc object.

def call_a_method_via_a_parameter( &some_method )
  # ?? call some_method and pass it a parameter ??
end
def my_method( param )
  puts param
end

call_a_method_via_a_parameter( &:my_method )
like image 284
RobertJoseph Avatar asked May 13 '26 00:05

RobertJoseph


1 Answers

def bob(jeff)
  puts "hi #{jeff}"
end

def mary(meth, params)
  meth.call(params)
end

mary(method(:bob), 'yo')
=> hi yo
like image 144
Michael Johnston Avatar answered May 14 '26 14:05

Michael Johnston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!