Dynamic object creation

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a
string how do I create an instance of the class the string contains?

How about this ?

s=“Array”
aObject=eval(s+“.new”)

···

On Mon, 30 Jun 2003 16:49:05 +0000, Aryeh Friedman wrote:

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a
string how do I create an instance of the class the string contains?


Simon Strandgaard

A class name is just a constant, so something like

a = Module.const_get(“Array”).new

is what you need (Aside: what’s the proper way to access ‘the top-level
Module’, or is the above correct?)

Cheers,

Brian.

···

On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a
string how do I create an instance of the class the string contains?

s = “Array”
aObject = eval(s).new

···

On Mon, 30 Jun 2003 15:49:05 +0000, Aryeh Friedman wrote:

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a
string how do I create an instance of the class the string contains?


ste

… and just hope nobody passes in s = “rm -rf /*” as a class name :expressionless:

const_get will be much faster too, as it doesn’t have to compile a piece of
code each time.

···

On Tue, Jul 01, 2003 at 01:22:22AM +0900, stefano wrote:

On Mon, 30 Jun 2003 15:49:05 +0000, Aryeh Friedman wrote:

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a
string how do I create an instance of the class the string contains?

s = “Array”
aObject = eval(s).new

Brian Candler wrote:

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a
string how do I create an instance of the class the string contains?

A class name is just a constant, so something like

a = Module.const_get(“Array”).new

uhm… why does this work?

I just tested it and to my surprise it worked?!

http://rubycentral.com/book/ref_c_module.html#Module.const_get

says that I need to supply a symbol, why does it accept a string?

is this something that will continue to work in 1.8? (im still using
1.6.8 on my winbox and 1.6.7 on my linux box)

···

On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:

is what you need (Aside: what’s the proper way to access ‘the top-level
Module’, or is the above correct?)

Cheers,

Brian.


dc -e
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap

“Brian Candler” B.Candler@pobox.com schrieb im Newsbeitrag
news:20030630171414.A19372@linnet.org

···

On Tue, Jul 01, 2003 at 01:01:25AM +0900, Aryeh Friedman wrote:

If I have something like this:

s=“Array”

How whould I get something like this:

aObject=s.new

In “plain” english I am asking if I have a valid name of a class in a
string how do I create an instance of the class the string contains?

A class name is just a constant, so something like

a = Module.const_get(“Array”).new

is what you need (Aside: what’s the proper way to access ‘the top-level
Module’, or is the above correct?)

I think this does it

a = Kernel.const_get(“Array”).new

robert

On the page you referenced, the entry for const_defined? says it takes
a symbol too, but the example code uses a string argument…!? Same for
const_set.

···

On Monday, Jun 30, 2003, at 15:23 America/Chicago, Anders Borch wrote:

A class name is just a constant, so something like
a = Module.const_get(“Array”).new

uhm… why does this work?

I just tested it and to my surprise it worked?!

http://rubycentral.com/book/ref_c_module.html#Module.const_get

says that I need to supply a symbol, why does it accept a string?

is this something that will continue to work in 1.8? (im still using
1.6.8 on my winbox and 1.6.7 on my linux box)


John Platte
Principal Consultant, NIKA Consulting
http://nikaconsulting.com/
(630) 499 9830
Fax: (630) 566 0655

… and just hope nobody passes in s = “rm -rf /*” as a class name :expressionless:

obviously, if you don’t check what you’re passing to eval(), you deserve
all the bad things that could happen :slight_smile:

const_get will be much faster too, as it doesn’t have to compile a piece of
code each time.

that’s true, I didn’t think about it.

···

On Tue, 01 Jul 2003 05:01:08 +0900, Brian Candler wrote:


ste

Even if it didn’t, you can easily convert strings to symbols and vice versa.

"Array".intern
 => :Array

:Array.to_s
 => "Array"

Regards,

Brian.

···

On Tue, Jul 01, 2003 at 05:23:35AM +0900, Anders Borch wrote:

A class name is just a constant, so something like

a = Module.const_get(“Array”).new

uhm… why does this work?

I just tested it and to my surprise it worked?!

http://rubycentral.com/book/ref_c_module.html#Module.const_get

says that I need to supply a symbol, why does it accept a string?

I think this does it

a = Kernel.const_get("Array").new

svg% ruby -e 'Kernel::Array; Object::Array'
-e:1: warning: toplevel constant Array referenced by Kernel::Array
svg%

Guy Decoux

John Platte wrote:

On the page you referenced, the entry for const_defined? says it takes a
symbol too, but the example code uses a string argument…!? Same for
const_set.

I just tested it and to my surprise it worked?!

http://rubycentral.com/book/ref_c_module.html#Module.const_get

says that I need to supply a symbol, why does it accept a string?

At the top of the section, you’ll find “In the descriptions that follow,
the parameter aSymbol refers to a symbol, which is either a quoted
string or a Symbol (such as :name).”

Cheers

Dave

http://rubycentral.com/book/ref_c_module.htm

I spoke too soon. At the top of the page:

In the descriptions that follow, the parameter aSymbol refers to a
symbol, which is either a quoted string or a Symbol (such as :name )

Sorry for the spurious post.

···

On Monday, Jun 30, 2003, at 15:33 America/Chicago, John Platte wrote:

On the page you referenced, the entry for const_defined? says it takes
a symbol too, but the example code uses a string argument…!? Same
for const_set.

On Monday, Jun 30, 2003, at 15:23 America/Chicago, Anders Borch wrote:

A class name is just a constant, so something like
a = Module.const_get(“Array”).new

uhm… why does this work?

I just tested it and to my surprise it worked?!

http://rubycentral.com/book/ref_c_module.html#Module.const_get

says that I need to supply a symbol, why does it accept a string?

is this something that will continue to work in 1.8? (im still using
1.6.8 on my winbox and 1.6.7 on my linux box)


John Platte
Principal Consultant, NIKA Consulting
http://nikaconsulting.com/
(630) 499 9830
Fax: (630) 566 0655

Brian Candler wrote:

A class name is just a constant, so something like

a = Module.const_get(“Array”).new

uhm… why does this work?

I just tested it and to my surprise it worked?!

http://rubycentral.com/book/ref_c_module.html#Module.const_get

says that I need to supply a symbol, why does it accept a string?

Even if it didn’t, you can easily convert strings to symbols and vice versa.

"Array".intern
 => :Array

hadn’t seen that one, thanks =)

···

On Tue, Jul 01, 2003 at 05:23:35AM +0900, Anders Borch wrote:

:Array.to_s
 => "Array"

Regards,

Brian.


dc -e
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap

Dave Thomas wrote:

John Platte wrote:

On the page you referenced, the entry for const_defined? says it takes
a symbol too, but the example code uses a string argument…!? Same
for const_set.

I just tested it and to my surprise it worked?!

http://rubycentral.com/book/ref_c_module.html#Module.const_get

says that I need to supply a symbol, why does it accept a string?

At the top of the section, you’ll find “In the descriptions that follow,
the parameter aSymbol refers to a symbol, which is either a quoted
string or a Symbol (such as :name).”

thanks alot :slight_smile: I’ll try to read from now on :wink:

···


dc -e
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap