[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

get a class from its name

Rolando Abarca

8/21/2007 12:17:00 AM

This is how i'm currently getting a class object from a name:

cname = "A::B::C::D"
klass = cname.split("::").inject(Kernel) {|a,b| a.const_get(b)}

I think it's pretty elegant, but I'm always happy to hear other ways
to do it ;-)
Anyone does this in some other way? (I don't want to eval code...)

regards,
--
Rolando Abarca
Phone: +56-9 97851962


6 Answers

Sam Smoot

8/21/2007 12:48:00 AM

0

On Aug 20, 7:16 pm, Rolando Abarca <funkas...@gmail.com> wrote:
> This is how i'm currently getting a class object from a name:
>
> cname = "A::B::C::D"
> klass = cname.split("::").inject(Kernel) {|a,b| a.const_get(b)}
>
> I think it's pretty elegant, but I'm always happy to hear other ways
> to do it ;-)
> Anyone does this in some other way? (I don't want to eval code...)
>
> regards,
> --
> Rolando Abarca
> Phone: +56-9 97851962
>
> PGP.sig
> 1KDownload

I've seen little helper methods for this, but never so elegant. Nice!

David A. Black

8/21/2007 1:42:00 AM

0

Tim Pease

8/21/2007 2:44:00 AM

0

On 8/20/07, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
> On Tue, 21 Aug 2007, Rolando Abarca wrote:
>
> > This is how i'm currently getting a class object from a name:
> >
> > cname = "A::B::C::D"
> > klass = cname.split("::").inject(Kernel) {|a,b| a.const_get(b)}
> >
> > I think it's pretty elegant, but I'm always happy to hear other ways to do it
> > ;-)
> > Anyone does this in some other way? (I don't want to eval code...)
>
> I think it's usually done the way you've got it there. It's one of
> those things that people write over and over (like map_with_index and
> singleton_method), hopefully some day to enter the core as
> const_get_deep or something.
>

"A::B::C::D".to_class

It could just be a wrapper for the core method rb_path2class -- which
does the same thing in Ruby C API land.

Blessings,
TwP

David A. Black

8/21/2007 11:42:00 AM

0

Robert Klemme

8/21/2007 2:41:00 PM

0

2007/8/21, David A. Black <dblack@rubypal.com>:
> Hi --
>
> On Tue, 21 Aug 2007, Tim Pease wrote:
> > "A::B::C::D".to_class
> >
> > It could just be a wrapper for the core method rb_path2class -- which
> > does the same thing in Ruby C API land.
>
> It's a more general-purpose thing, though; it's for any constant, not
> just classes. It's really just a variation on const_get, which already
> will give you any constant from any existing class/module nesting.

Definitively.

Folks, please keep in mind that the proposed solution from the OP's
posting works only in the global context:

irb(main):001:0> module A
irb(main):002:1> module B
irb(main):003:2> module C
irb(main):004:3> end
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> "A::B::C".split("::").inject(Kernel) {|a,b| a.const_get(b)}
=> A::B::C
irb(main):008:0> module A
irb(main):009:1> p "B::C".split("::").inject(Kernel) {|a,b| a.const_get(b)}
irb(main):010:1> end
NameError: uninitialized constant Kernel::B
from (irb):9:in `const_get'
from (irb):9
from (irb):10:in `inject'
from (irb):9:in `each'
from (irb):9:in `inject'
from (irb):9
from :0
irb(main):011:0> module A
irb(main):012:1> p "B::C".split("::").inject(self) {|a,b| a.const_get(b)}
irb(main):013:1> end
A::B::C
=> nil

There needs to be at least a bit more logic to take care of nested
scopes and constants with leading "::". Having said that it's probably
wise to put the method in class Module, like:

class Module
def deref(name)
parts = name.split "::"
start = self

if parts.first == ""
# start with global scope
start = Kernel
parts.shift
end

parts.inject(start) {|a,b| a.const_get(b)}
end
end

class Object
def deref(name)
self.class.deref(name)
end
end

irb(main):021:0> module A
irb(main):022:1> module B
irb(main):023:2> module C
irb(main):024:3> end
irb(main):025:2> end
irb(main):026:1> end
=> nil
irb(main):027:0> deref "A::B::C"
=> A::B::C
irb(main):028:0> module A
irb(main):029:1> p deref("B::C")
irb(main):030:1> p deref("::A::B")
irb(main):031:1> end
A::B::C
A::B
=> nil


Kind regards

robert

Rolando Abarca

8/21/2007 3:38:00 PM

0

On Aug 21, 2007, at 10:41 AM, Robert Klemme wrote:

> Definitively.
>
> Folks, please keep in mind that the proposed solution from the OP's
> posting works only in the global context:

Ok... I get your point. It's not a general dereferencer, i mean, it
doesn't take the current scope and figure it outs the name from
there, it always assume the string is the full path... Maybe what you
propose could be the general solution to the deref problem :-)

> robert

regards,
--
Rolando Abarca
Phone: +56-9 97851962