tags:

views:

1342

answers:

1

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

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?

+8  A: 

With the method method... =)

f.method(:a).aa
Ola Bini
I had no idea that the method method existed. Handy. Thanks.
Squeegy