Hey All,
If I have the name of a class in a string var, is it possible to get a
new instance of that class (and if so, how)? The following do *not*
work:
my_string = "Hash"
my_object = Class(my_string)
my_object = Class.new(my_string)
my_object = Object.new(my_string)
Thanks!
-Roy
F_Senault
(F. Senault)
24 January 2009 01:28
2
my_object = Object.const_get(my_string.to_sym).new
(The to_sym is mandatory in ruby 1.9.)
Fred
···
Le 24 janvier 2009 à 02:12, rpardee@gmail.com a écrit :
Hey All,
If I have the name of a class in a string var, is it possible to get a
new instance of that class (and if so, how)? The following do *not*
work:
my_string = "Hash"
my_object = Class(my_string)
my_object = Class.new(my_string)
my_object = Object.new(my_string)
--
Any time tomorrow a part of me will die
And a new one will be born
Any time tomorrow I'll get sick of asking why
Sick of all the darkness I have worn (K's Choice, Shadow Man)
"rpardee@gmail.com" <rpardee@gmail.com> writes:
Hey All,
If I have the name of a class in a string var, is it possible to get a
new instance of that class (and if so, how)? The following do *not*
work:
my_string = "Hash"
my_object = Class(my_string)
my_object = Class.new(my_string)
my_object = Object.new(my_string)
Class names are used to define constants in the Module module:
irb(main):014:0> className="Hash"
"Hash"
irb(main):015:0> Module.constants.member?(className)
true
Therefore you should be able to get the class with:
irb(main):016:0> Module.class_eval(className)
Hash
and make an instance with:
irb(main):017:0> Module.class_eval(className).new
{}
···
--
__Pascal Bourguignon__
Awesome cool--looks like I can even pass arguments into new() (which
would have been my next question).
Thanks very much!
-Roy
···
On Jan 23, 5:25 pm, "F. Senault" <f...@lacave.net> wrote:
Le 24 janvier 2009 02:12, rpar...@gmail.com a crit :
> Hey All,
> If I have the name of a class in a string var, is it possible to get a
> new instance of that class (and if so, how)? The following do *not*
> work:
> my_string = "Hash"
> my_object = Class(my_string)
> my_object = Class.new(my_string)
> my_object = Object.new(my_string)
my_object = Object.const_get(my_string.to_sym).new
(The to_sym is mandatory in ruby 1.9.)
Fred
--
Any time tomorrow a part of me will die
And a new one will be born
Any time tomorrow I'll get sick of asking why
Sick of all the darkness I have worn (K's Choice, Shadow Man)
Na.
$ ruby_dev -ve 'p File.const_get("Stat")'
ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21203) [i386-darwin9.6.0]
File::Stat
Ruby's a pretty smart gal.
James Edward Gray II
···
On Jan 23, 2009, at 7:28 PM, F. Senault wrote:
my_object = Object.const_get(my_string.to_sym).new
(The to_sym is mandatory in ruby 1.9.)
F_Senault
(F. Senault)
27 January 2009 18:12
7
You are, of course, perfectly right. I should do my tests _after_ the
coffee...
Fred
···
Le 24 janvier à 03:16, James Gray a écrit :
On Jan 23, 2009, at 7:28 PM, F. Senault wrote:
my_object = Object.const_get(my_string.to_sym).new
(The to_sym is mandatory in ruby 1.9.)
Na.
--
GOTO and DO statements considered offensively authoritarian; we suggest
you replace them with "please visit" and "would you like to" statements.
(Tanuki in the SDM, on politically-correct coding)