如何获取方法的Method对象?

时间:2022-12-06 07:31:26

I'm trying to extend the Method class along the lines of:

我正在尝试按以下方式扩展Method类:

irb(main):008:0> class A
irb(main):009:1> def a
irb(main):010:2> puts "blah"
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> class Method
irb(main):014:1> def aa
irb(main):015:2> p "hi"
irb(main):016:2> end
irb(main):017:1> end
=> nil
irb(main):018:0> f = A.new
=> #<A:0x54ed4>
irb(main):019:0> A.a
NoMethodError: undefined method `a' for A:Class
    from (irb):19
    from :0
irb(main):020:0> f.a
blah
=> nil
irb(main):027:0> f.a.aa
blah
NoMethodError: undefined method `aa' for nil:NilClass
    from (irb):27
    from :0

As expected, when I f.a.aa, the .aa is being executed on the return value of f.a. How do I gain access to the Method object which represents f.a?

正如预期的那样,当我f.a.aa时,.aa正在f.a的返回值上执行。如何获取表示f.a的Method对象的访问权限?

1 个解决方案

#1


31  

With the method method... =)

用方法方法... =)

f.method(:a).aa

#1


31  

With the method method... =)

用方法方法... =)

f.method(:a).aa