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").
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::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
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::foo").call
=> "foo"
You might want to use '.foo' and split on /\./ to get the method...