Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting a method object directly from a module

0 views
Skip to first unread message

Daniel Berger

unread,
Jun 9, 2005, 2:52:41 PM6/9/05
to
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

Kent Sibilev

unread,
Jun 9, 2005, 3:02:24 PM6/9/05
to
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

unread,
Jun 9, 2005, 3:07:05 PM6/9/05
to
Or if you want to be able to call the method:

m = Object.new.extend(Foo).method(:my_method)

Ryan

Daniel Berger

unread,
Jun 9, 2005, 3:14:39 PM6/9/05
to

That works great. Thanks both.

Dan

Yukihiro Matsumoto

unread,
Jun 9, 2005, 7:48:14 PM6/9/05
to

In message "Re: Getting a method object directly from a module"

on Fri, 10 Jun 2005 04:07:05 +0900, "Ryan Leavengood" <mrc...@netrox.net> writes:
|
|Or if you want to be able to call the method:
|
|m = Object.new.extend(Foo).method(:my_method)

Or you can bind unbound method:

Foo.instance_method(:my_method).bind(obj).call

matz.


0 new messages