Dynamic class' name

Dear People,

Can i use an arbitrary string (i.e. read from the config file) as a class’
name to call it’s new(), etc. knowing that i have that class’ definition?

···


Yours truly, WBR, Paul Argentoff.

Can i use an arbitrary string (i.e. read from the config file) as a class'
name to call it's new(), etc. knowing that i have that class' definition?

a class name begin with a capital letter, this mean that it's a
constant. Retrieve the constant (with const_get) and you have the
class. For example

pigeon% ruby -e 'p Object.const_get("Array").new'

pigeon%

pigeon% ruby -e 'p File.const_get("Stat").new("b.rb")'
#<File::Stat dev=0x301, ino=688436, mode=0100755, nlink=1, uid=1000,
gid=1000, rdev=0x0, size=75, blksize=4096, blocks=2, atime=Mon Mar 24
12:57:10 CET 2003, mtime=Mon Mar 24 12:55:42 CET 2003, ctime=Mon Mar 24
12:55:42 CET 2003>
pigeon%

Guy Decoux

That’s it, thnx.

···

On Mon, Mar 24, 2003 at 10:00:44PM +0900, ts wrote:

pigeon% ruby -e ‘p Object.const_get(“Array”).new’

pigeon%


Yours truly, WBR, Paul Argentoff.