String to Class?

A

Aaron Turner

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://synfin.net/
http://tcpreplay.synfin.net/ - 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
 
G

Gordon Thiesfeld

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://synfin.net/
http://tcpreplay.synfin.net/ - 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
 
S

Stefano Crocco

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
 
I

Ilan Berci

Stefano said:
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
 
F

fedzor

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.
 
S

sishen

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top