Forum: Ruby instantiate a class dynamically

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87168b6a1a40c69f8f2ddfc9c42ad71c?d=identicon&s=25 rpardee@gmail.com (Guest)
on 2009-01-24 02:15
(Received via mailing list)
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
B57c5af36f5c1f33243dd8b2dd9043b1?d=identicon&s=25 F. Senault (Guest)
on 2009-01-24 02:30
(Received via mailing list)
Le 24 janvier 2009 à 02:12, rpardee@gmail.com a écrit :

>   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
87168b6a1a40c69f8f2ddfc9c42ad71c?d=identicon&s=25 rpardee@gmail.com (Guest)
on 2009-01-24 02:31
(Received via mailing list)
On Jan 23, 5:25 pm, "F. Senault" <f...@lacave.net> wrote:
> >   my_object = Class(my_string)
> 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)

Awesome cool--looks like I can even pass arguments into new() (which
would have been my next question).

Thanks very much!

-Roy
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-24 02:44
(Received via mailing list)
Hi --

On Sat, 24 Jan 2009, F. Senault wrote:

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

I don't think so:

   $ ruby19 -ve 'p Object.const_get("String")'
   ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21203)
[i386-darwin9.5.0]
   String


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-01-24 02:45
(Received via mailing list)
"rpardee@gmail.com" <rpardee@gmail.com> writes:

>   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
{}
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-01-24 03:23
(Received via mailing list)
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.

   $ 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
B57c5af36f5c1f33243dd8b2dd9043b1?d=identicon&s=25 F. Senault (Guest)
on 2009-01-27 19:15
(Received via mailing list)
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.

You are, of course, perfectly right.  I should do my tests _after_ the
coffee...  :)

Fred
This topic is locked and can not be replied to.