Method name to method object

I want to obtain method objects given their string names. I can't figure out
how to get the method object for MyMethod::mymeth given the string name
"MyMethod::mymeth". I don't know the name of the module in advance, or even
if any was given (or even how many levels of modules deep the method is), so
I can't use something like MyMethod::method("mymeth").

How does one do that?

Sean O'Dell

I want to obtain method objects given their string names. I can't figure out
how to get the method object for MyMethod::mymeth given the string name
"MyMethod::mymeth". I don't know the name of the module in advance, or even
if any was given (or even how many levels of modules deep the method is), so
I can't use something like MyMethod::method("mymeth").

How does one do that?

module A; module B; def self.foo; "foo" end end end

=> nil

def get_smethod(str); a=str.split(/::/); m=a.pop; a.inject(Object){|s,x|s.const_get(x)}.method(m) end

=> nil

get_smethod("A::b::foo").call

=> "foo"

You might want to use '.foo' and split on /\./ to get the method...

···

On Sat, Aug 28, 2004 at 04:42:40AM +0900, Sean O'Dell wrote:

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

"Sean O'Dell" <sean@celsoft.com> schrieb im Newsbeitrag
news:200408271242.37206.sean@celsoft.com...

I want to obtain method objects given their string names. I can't figure

out

how to get the method object for MyMethod::mymeth given the string name
"MyMethod::mymeth". I don't know the name of the module in advance, or

even

if any was given (or even how many levels of modules deep the method is),

so

I can't use something like MyMethod::method("mymeth").

How does one do that?

Sean O'Dell

class Foo;def bar;end;end

=> nil

Foo.instance_method :bar

=> #<UnboundMethod: Foo#bar>

Foo.instance_method "bar"

=> #<UnboundMethod: Foo#bar>

Foo.new.method :bar

=> #<Method: Foo#bar>

Foo.new.method "bar"

=> #<Method: Foo#bar>

Kind regards

    robert

Works very well, thank you. I didn't realize that module/class nestings were
accessible as constants.

Sean O'Dell

···

On Friday 27 August 2004 12:50, Mauricio Fernández wrote:

On Sat, Aug 28, 2004 at 04:42:40AM +0900, Sean O'Dell wrote:
> I want to obtain method objects given their string names. I can't figure
> out how to get the method object for MyMethod::mymeth given the string
> name "MyMethod::mymeth". I don't know the name of the module in advance,
> or even if any was given (or even how many levels of modules deep the
> method is), so I can't use something like MyMethod::method("mymeth").
>
> How does one do that?
>
>> module A; module B; def self.foo; "foo" end end end

=> nil

>> def get_smethod(str); a=str.split(/::/); m=a.pop;
>> a.inject(Object){|s,x|s.const_get(x)}.method(m) end

=> nil

>> get_smethod("A::b::foo").call

=> "foo"

You might want to use '.foo' and split on /\./ to get the method...