[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting a method object directly from a module

djberg96

6/9/2005 6:53:00 PM

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

4 Answers

Kent Sibilev

6/9/2005 7:02:00 PM

0

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
>
>
>


Ryan Leavengood

6/9/2005 7:07:00 PM

0

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.


djberg96

6/9/2005 7:15:00 PM

0

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

Yukihiro Matsumoto

6/9/2005 11:48:00 PM

0


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)

Or you can bind unbound method:

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

matz.