Idiomatic way to convert class method to proc in ruby Idiomatic way to convert class method to proc in ruby ruby ruby

Idiomatic way to convert class method to proc in ruby


Would method be what you're looking for? It can let you save a method to a variable.

2.1.0 :003 > m = Kernel.method(:puts) => #<Method: Kernel.puts>2.1.0 :004 > m.call('hi')hi


I think you just want Object#method:

meth = Kernel.method(:puts)meth["hello"]# => hello


You can pass the receiver object as first parameter, and actual argument as subsequent parameters.

:puts.to_proc.call(Kernel, "Hi")#=> Hi

I found this article - RUBY: SYMBOL#TO_PROC IS A LAMBADASS - to be quite informative on behavior of lambdas returned by Symbol#to_proc