[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String to Class?

Aaron Turner

2/28/2008 5:29:00 PM

I want to be able to take a class name stored in a variable as a string like:

foo = "Foo::Bar"

and convert that to the class Foo::Bar so that I can call .new(), etc:

obj = foo.new

I know I can just do:

foo = Foo::Bar

but that doesn't solve my problem which is to accept the class name as
an argument on the command line from the user.

Thanks,
Aaron

--
Aaron Turner
http://s...
http://tcpreplay.s... - Pcap editing & replay tools for Unix
They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety. -- Benjamin Franklin

5 Answers

Gordon Thiesfeld

2/28/2008 5:42:00 PM

0

On Thu, Feb 28, 2008 at 11:28 AM, Aaron Turner <synfinatic@gmail.com> wrote:
> I want to be able to take a class name stored in a variable as a string like:
>
> foo = "Foo::Bar"
>
> and convert that to the class Foo::Bar so that I can call .new(), etc:
>
> obj = foo.new
>
> I know I can just do:
>
> foo = Foo::Bar
>
> but that doesn't solve my problem which is to accept the class name as
> an argument on the command line from the user.
>
> Thanks,
> Aaron
>
> --
> Aaron Turner
> http://s...
> http://tcpreplay.s... - Pcap editing & replay tools for Unix
> They that can give up essential liberty to obtain a little temporary
> safety deserve neither liberty nor safety. -- Benjamin Franklin
>
>


I'm sure there's a better way, but:

module Foo
class Bar

end
end

foo = "Foo::Bar"
foo = foo.split("::").inject(Object){|s,v| s.const_get(v) }

obj = foo.new

p obj

Stefano Crocco

2/28/2008 5:48:00 PM

0

Alle Thursday 28 February 2008, Aaron Turner ha scritto:
> I want to be able to take a class name stored in a variable as a string
> like:
>
> foo = "Foo::Bar"
>
> and convert that to the class Foo::Bar so that I can call .new(), etc:
>
> obj = foo.new
>
> I know I can just do:
>
> foo = Foo::Bar
>
> but that doesn't solve my problem which is to accept the class name as
> an argument on the command line from the user.
>
> Thanks,
> Aaron

foo.split('::').inject(Kernel){|res, i| res.const_get(i)}

Stefano


Ilan Berci

2/28/2008 11:53:00 PM

0

Stefano Crocco wrote:

>
> foo.split('::').inject(Kernel){|res, i| res.const_get(i)}
>
> Stefano

Gordon, Stefano,

That is a SUPER cool form of inject()! I usually just use is as a
builder for a hash or other container type, but having inject using
something fairly different on each iteration is grand! Why didn't I
think of that???!!!

Great tip, thanks again

ilan


--
Posted via http://www.ruby-....

fedzor

2/29/2008 12:17:00 AM

0


On Feb 28, 2008, at 12:47 PM, Stefano Crocco wrote:

> foo.split('::').inject(Kernel){|res, i| res.const_get(i)}

That is the coolest thing i have ever seen. Seriously. It will take a
lot of awesome to beat this!

+1

_______________________________|
- Ari
I just bought another case of RockStar. Expect architectural changes.



Dingding Ye

2/29/2008 12:36:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

cool~ I haven't thought it before.

Rails have its owner ext for this function classed Inflector.constantize.
And it's implementation is used module_eval.
So it can also be :

obj = Object.module_eval(foo).new



On Fri, Feb 29, 2008 at 1:47 AM, Stefano Crocco <stefano.crocco@alice.it>
wrote:

> Alle Thursday 28 February 2008, Aaron Turner ha scritto:
> > I want to be able to take a class name stored in a variable as a string
> > like:
> >
> > foo = "Foo::Bar"
> >
> > and convert that to the class Foo::Bar so that I can call .new(), etc:
> >
> > obj = foo.new
> >
> > I know I can just do:
> >
> > foo = Foo::Bar
> >
> > but that doesn't solve my problem which is to accept the class name as
> > an argument on the command line from the user.
> >
> > Thanks,
> > Aaron
>
> foo.split('::').inject(Kernel){|res, i| res.const_get(i)}
>
> Stefano
>
>
>