简体   繁体   中英

How to convert method to proc for &method usage in Ruby

For learning purposes, I'm exploring if I can convert these three method calls:

Foo::Bar.logger.debug(a)
Foo::Bar.logger.debug(b)
Foo::Bar.logger.debug(c)

into a single-line statement using the shorthand proc &method approach:

[a, b, c].each(&method(:'Foo::Bar.logger.debug'))

.debug does not respond to .to_proc so naturally:

NameError: undefined method `Foo::Bar.logger.debug' for class `#<Class:Foo>'

This does work; however, but isn't as succinct as the former:

logger = Proc.new { |x| Foo::Bar.logger.debug(x) }                                                                                                           
[a, b, c].each(&logger)

Is it possible for the former approach to work?

You're using the method method incorrectly. From the fine manual :

method(sym) → method
Looks up the named method as a receiver in obj , returning a Method object (or raising NameError ).

You'd usually say

m = some_obj.method(:some_method_name)

and then work with m . Saying:

method(:Foo::Bar.logger.debug)

should be giving you a TypeError because because :Foo is a symbol, not a class or module and trying to apply :: to a symbol makes no sense. I suspect that you're actually saying:

method(':Foo::Bar.logger.debug')

as that will produce the error you're seeing. Assuming that that's the case, then you're actually trying to get a reference to the method named ':Foo::Bar.logger.debug' in the object self .

If you want a reference to the debug method in Foo::Bar.logger then you'd say:

Foo::Bar.logger.method(:debug)

Combining that your to_proc call (via & ):

[a, b, c].each(&Foo::Bar.logger.method(:debug))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM