Hi all,
Is it possible to get a method object from a Module directly? This
doesn't work:
module Foo
def my_method
end
end
method = Foo.method(:my_method)
=> NameError: undefined method `my_method' for class `Module'
Is there a way to do what I mean?
Regards,
Dan
irb(main):001:0> module M
irb(main):002:1> def m
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> M.instance_method(:m)
=> #<UnboundMethod: M#m>
Kent.
···
On 6/9/05, Daniel Berger <djberg96@hotmail.com> wrote:
Hi all,
Is it possible to get a method object from a Module directly? This
doesn't work:
module Foo
def my_method
end
end
method = Foo.method(:my_method)
=> NameError: undefined method `my_method' for class `Module'
Is there a way to do what I mean?
Regards,
Dan
Or if you want to be able to call the method:
m = Object.new.extend(Foo).method(:my_method)
Ryan
Kent Sibilev said:
···
irb(main):001:0> module M
irb(main):002:1> def m
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> M.instance_method(:m)
=> #<UnboundMethod: M#m>
Kent.
Ryan Leavengood wrote:
Or if you want to be able to call the method:
m = Object.new.extend(Foo).method(:my_method)
Ryan
Kent Sibilev said:
> irb(main):001:0> module M
> irb(main):002:1> def m
> irb(main):003:2> end
> irb(main):004:1> end
> => nil
> irb(main):005:0> M.instance_method(:m)
> => #<UnboundMethod: M#m>
>
> Kent.
That works great. Thanks both.
Dan
Or you can bind unbound method:
Foo.instance_method(:my_method).bind(obj).call
matz.
···
In message "Re: Getting a method object directly from a module" on Fri, 10 Jun 2005 04:07:05 +0900, "Ryan Leavengood" <mrcode@netrox.net> writes:
Or if you want to be able to call the method:
m = Object.new.extend(Foo).method(:my_method)