From: Sean O'Dell Date: 2004-08-28T05:00:00+09:00 Subject: Re: Method name to method object 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... Works very well, thank you. I didn't realize that module/class nestings were accessible as constants. Sean O'Dell