# Instance from class name?

**URL:** <https://rubytalk.org/t/instance-from-class-name/21537>\
**Category:** ruby-talk\
**Created:** [13 October 2005 13:34 UTC](https://rubytalk.org/t/instance-from-class-name/21537 "2005-10-13T13:34:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Lamb](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Alexander\_Lamb](https://rubytalk.org/u/Alexander_Lamb)\
**Post date:** [13 October 2005 13:34 UTC](https://rubytalk.org/t/instance-from-class-name/21537/1 "2005-10-13T13:34:03Z")

</div>

Hello,

I am new to this list (and language) but I didn't find my answer from the documentation available.

I simply would like to do something like:

myObject = class.fromName("a\_class\_name").new

I can do this in Java and Objective-C but didn't find how in Ruby. The idea is to have some configuration files for an activity which contain a class name to instantiate depending on the activity.

Many thanks (and sorry if this is an obvious question),

> **···**
>
> --  
> Alexander Lamb  
> Service d'Informatique Médicale  
> Hôpitaux Universitaires de Genève  
> [Alexander.J.Lamb@sim.hcuge.ch](mailto:Alexander.J.Lamb@sim.hcuge.ch)  
> +41 22 372 88 62  
> +41 79 420 79 73

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 October 2005 13:37 UTC](https://rubytalk.org/t/instance-from-class-name/21537/2 "2005-10-13T13:37:11Z")

</div>

Hi --

> **···**
>
> On Thu, 13 Oct 2005, Alexander Lamb wrote:
> 
> > Hello,
> > 
> > I am new to this list (and language) but I didn't find my answer from the documentation available.
> > 
> > I simply would like to do something like:
> > 
> > myObject = class.fromName("a\_class\_name").new
> 
> my\_object = Object.const\_get("MyClass").new
> 
> or some variation of that.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Anonymous\_Coward](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Anonymous\_Coward](https://rubytalk.org/u/Anonymous_Coward)\
**Post date:** [13 October 2005 19:13 UTC](https://rubytalk.org/t/instance-from-class-name/21537/3 "2005-10-13T19:13:40Z")

</div>

David A. Black wrote:

> Hi --
> 
> > Hello,
> > 
> > I am new to this list (and language) but I didn't find my answer from the documentation available.
> > 
> > I simply would like to do something like:
> > 
> > myObject = class.fromName("a\_class\_name").new

Classes are stored as constants in ruby. Witness Mr. Black's solution:

> my\_object = Object.const\_get("MyClass").new
> 
> or some variation of that.

That works; however, if your class name looks like SomeModule::SomeClass,  
you will need to be a bit fancier because const\_get only looks for constants  
in the current class or module (the top-level Object instance is the default):

&nbsp;&nbsp;&nbsp;class\_name.split('::').inject(Object) {|parent, obj| parent.const\_get obj}.new

#split creates an array ['SomeModule', 'SomeClass'], which we iterate over  
using #inject. In the #inject block, the parent will always be the parent  
class or module in which we look for the constant and obj is the next class  
or module name. The Object is passed in as the initial parent object. the  
parent.const\_get obj sequence then looks like

&nbsp;&nbsp;&nbsp;Object.const\_get 'SomeModule' =\> =\> SomeModule.const\_get 'SomeClass'

There may be a method to directly get the class in the future whether it is  
nested or not. In the meanwhile, you can stick the above code in a method  
if you will be using it a lot.

Also, you can always use #eval:

&nbsp;&nbsp;&nbsp;obj = eval(class\_name).new

Only, of course, if you trust the source of the class\_name String. The  
former method is preferred, though.

> David

E

> **···**
>
> > On Thu, 13 Oct 2005, Alexander Lamb wrote:

---

<div class="post-metadata">

**Author:** ![Christophe\_Grandsire](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@Christophe\_Grandsire](https://rubytalk.org/u/Christophe_Grandsire)\
**Post date:** [13 October 2005 19:37 UTC](https://rubytalk.org/t/instance-from-class-name/21537/4 "2005-10-13T19:37:56Z")

</div>

En réponse à ES :

> That works; however, if your class name looks like SomeModule::SomeClass,  
> you will need to be a bit fancier because const\_get only looks for constants  
> in the current class or module (the top-level Object instance is the default):
> 
> &nbsp;&nbsp;class\_name.split('::').inject(Object) {|parent, obj| parent.const\_get obj}.new

Oh... my...!!! I thought I understood how inject works, but never would I have guessed that it was capable of such nifty things!!! This is... wow!

> #split creates an array ['SomeModule', 'SomeClass'], which we iterate over  
> using #inject. In the #inject block, the parent will always be the parent  
> class or module in which we look for the constant and obj is the next class  
> or module name. The Object is passed in as the initial parent object. the  
> parent.const\_get obj sequence then looks like
> 
> &nbsp;&nbsp;Object.const\_get 'SomeModule' =\> =\> SomeModule.const\_get 'SomeClass'

When you explain it like that it makes perfect sense. But still... wow!!!

> **···**
>
> --  
> Christophe Grandsire (who's falling in love with Ruby again 🙂 ).
> 
> [http://rainbow.conlang.free.fr](http://rainbow.conlang.free.fr)
> 
> You need a straight mind to invent a twisted conlang.

---

<div class="post-metadata">

**Author:** ![Alexander\_Lamb](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Alexander\_Lamb](https://rubytalk.org/u/Alexander_Lamb)\
**Post date:** [13 October 2005 19:44 UTC](https://rubytalk.org/t/instance-from-class-name/21537/5 "2005-10-13T19:44:11Z")

</div>

Thanks for those pointers. The first suggestion worked and since I can trust the String of the name of the class, it is ok.

However, I didn't find it in the documentation of the Object class. I started on ruby-lang.org then was brought to rubycentral for the reference.  
Is there another complete reference for ruby 1.8.x? (I am running on MacOSX 10.4.2).

Thanks,

> **···**
>
> --  
> Alexander Lamb  
> Service d'Informatique Médicale  
> Hôpitaux Universitaires de Genève  
> Alexander.J.Lamb@sim.hcuge.ch  
> +41 22 372 88 62  
> +41 79 420 79 73
> 
> On Oct 13, 2005, at 9:13 PM, ES wrote:
> 
> > David A. Black wrote:
> > 
> > > Hi --  
> > > On Thu, 13 Oct 2005, Alexander Lamb wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I am new to this list (and language) but I didn't find my answer from the documentation available.
> > > > 
> > > > I simply would like to do something like:
> > > > 
> > > > myObject = class.fromName("a\_class\_name").new
> > 
> > Classes are stored as constants in ruby. Witness Mr. Black's solution:
> > 
> > > my\_object = Object.const\_get("MyClass").new  
> > > or some variation of that.
> > 
> > That works; however, if your class name looks like SomeModule::SomeClass,  
> > you will need to be a bit fancier because const\_get only looks for constants  
> > in the current class or module (the top-level Object instance is the default):
> > 
> > &nbsp;&nbsp;class\_name.split('::').inject(Object) {|parent, obj| parent.const\_get obj}.new
> > 
> > #split creates an array ['SomeModule', 'SomeClass'], which we iterate over  
> > using #inject. In the #inject block, the parent will always be the parent  
> > class or module in which we look for the constant and obj is the next class  
> > or module name. The Object is passed in as the initial parent object. the  
> > parent.const\_get obj sequence then looks like
> > 
> > &nbsp;&nbsp;Object.const\_get 'SomeModule' =\> =\> SomeModule.const\_get 'SomeClass'
> > 
> > There may be a method to directly get the class in the future whether it is  
> > nested or not. In the meanwhile, you can stick the above code in a method  
> > if you will be using it a lot.
> > 
> > Also, you can always use #eval:
> > 
> > &nbsp;&nbsp;obj = eval(class\_name).new
> > 
> > Only, of course, if you trust the source of the class\_name String. The  
> > former method is preferred, though.
> > 
> > > David
> > 
> > E

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 October 2005 21:47 UTC](https://rubytalk.org/t/instance-from-class-name/21537/6 "2005-10-13T21:47:42Z")

</div>

Hi --

> **···**
>
> On Fri, 14 Oct 2005, ES wrote:
> 
> > David A. Black wrote:
> > 
> > > Hi --
> > > 
> > > On Thu, 13 Oct 2005, Alexander Lamb wrote:
> > > 
> > > > Hello,
> > > > 
> > > > I am new to this list (and language) but I didn't find my answer from the documentation available.
> > > > 
> > > > I simply would like to do something like:
> > > > 
> > > > myObject = class.fromName("a\_class\_name").new
> > 
> > Classes are stored as constants in ruby. Witness Mr. Black's solution:
> > 
> > > my\_object = Object.const\_get("MyClass").new
> > > 
> > > or some variation of that.
> > 
> > That works; however, if your class name looks like SomeModule::SomeClass,  
> > you will need to be a bit fancier because const\_get only looks for constants  
> > in the current class or module (the top-level Object instance is the default):
> > 
> > class\_name.split('::').inject(Object) {|parent, obj| parent.const\_get obj}.new
> 
> Like Kernel#singleton\_class, this is one of those things that gets  
> written again and again, ad hoc. I wonder if it might be a candidate  
> for a permanent modification to const\_get....
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Christophe\_Grandsire](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@Christophe\_Grandsire](https://rubytalk.org/u/Christophe_Grandsire)\
**Post date:** [13 October 2005 20:10 UTC](https://rubytalk.org/t/instance-from-class-name/21537/7 "2005-10-13T20:10:35Z")

</div>

En réponse à Alexander Lamb :

> However, I didn't find it in the documentation of the Object class. I started on ruby-lang.org then was brought to rubycentral for the reference.  
> Is there another complete reference for ruby 1.8.x? (I am running on MacOSX 10.4.2).

> **[Ruby-Doc.org: Documenting the Ruby Language](https://ruby-doc.org/)**
>
> Fast, searchable Ruby documentation for core and standard libraries. Plus, links to tutorials, guides, books, and related sites.

However, the best way is to use ri, the built-in documentation system for Ruby. Just type ri ClassOrModuleName#method to get the documentation for that instance method (without the #method, you get the documentation for the class or module itself, with lists of constants, class or module methods and instance methods, and for class or module methods the syntax is IIRC ri ClassOrModuleName::class\_or\_module\_method).

But anyway, const\_get isn't defined in Object but in Module. When you call Object.const\_get, you are calling an instance method of the object "Object", which is an object of class "Class". Since Class inherits from Module, it inherits its instance method "const\_get", which can thus be called on any object of class "Class", i.e. any class.

If you don't really understand it, do not fear. It took me a while to get it too. Just remember that in Ruby everything is an object, and that includes classes themselves 🙂 . So you can send methods to them too.

> **···**
>
> --  
> Christophe Grandsire.
> 
> [http://rainbow.conlang.free.fr](http://rainbow.conlang.free.fr)
> 
> You need a straight mind to invent a twisted conlang.

---

<div class="post-metadata">

**Author:** ![Anonymous\_Coward](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Anonymous\_Coward](https://rubytalk.org/u/Anonymous_Coward)\
**Post date:** [14 October 2005 02:12 UTC](https://rubytalk.org/t/instance-from-class-name/21537/8 "2005-10-14T02:12:06Z")

</div>

David A. Black wrote:

> Hi --
> 
> > David A. Black wrote:
> > 
> > > Hi --
> > > 
> > > > Hello,
> > > > 
> > > > I am new to this list (and language) but I didn't find my answer from the documentation available.
> > > > 
> > > > I simply would like to do something like:
> > > > 
> > > > myObject = class.fromName("a\_class\_name").new
> > 
> > Classes are stored as constants in ruby. Witness Mr. Black's solution:
> > 
> > > my\_object = Object.const\_get("MyClass").new
> > > 
> > > or some variation of that.
> > 
> > That works; however, if your class name looks like SomeModule::SomeClass,  
> > you will need to be a bit fancier because const\_get only looks for constants  
> > in the current class or module (the top-level Object instance is the default):
> > 
> > class\_name.split('::').inject(Object) {|parent, obj| parent.const\_get obj}.new
> 
> Like Kernel#singleton\_class, this is one of those things that gets  
> written again and again, ad hoc. I wonder if it might be a candidate  
> for a permanent modification to const\_get....

If only 🙂 Although I think that it might be more appropriate to have  
something like ObjectSpace.const\_get for that use.

> David

E

> **···**
>
> > On Fri, 14 Oct 2005, ES wrote:
> > 
> > > > On Thu, 13 Oct 2005, Alexander Lamb wrote:

---

<div class="post-metadata">

**Author:** ![Alexander\_Lamb](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Alexander\_Lamb](https://rubytalk.org/u/Alexander_Lamb)\
**Post date:** [14 October 2005 05:41 UTC](https://rubytalk.org/t/instance-from-class-name/21537/9 "2005-10-14T05:41:39Z")

</div>

Ah, I understand. Simply a class method (like in Objective-C). I just wasn't careful about the fact in inherited from Module (the class object hierarchy). This can be difficult to understand for Java developer who don't have real class objects and where static functions are not inherited.

Thanks,

> **···**
>
> --  
> Alexander Lamb  
> Service d'Informatique Médicale  
> Hôpitaux Universitaires de Genève  
> Alexander.J.Lamb@sim.hcuge.ch  
> +41 22 372 88 62  
> +41 79 420 79 73
> 
> On Oct 13, 2005, at 10:10 PM, Christophe Grandsire wrote:
> 
> > En réponse à Alexander Lamb :
> > 
> > > However, I didn't find it in the documentation of the Object class. I started on ruby-lang.org then was brought to rubycentral for the reference.  
> > > Is there another complete reference for ruby 1.8.x? (I am running on MacOSX 10.4.2).
> > 
> > [http://www.ruby-doc.org/](http://www.ruby-doc.org/)  
> > However, the best way is to use ri, the built-in documentation system for Ruby. Just type ri ClassOrModuleName#method to get the documentation for that instance method (without the #method, you get the documentation for the class or module itself, with lists of constants, class or module methods and instance methods, and for class or module methods the syntax is IIRC ri ClassOrModuleName::class\_or\_module\_method).
> > 
> > But anyway, const\_get isn't defined in Object but in Module. When you call Object.const\_get, you are calling an instance method of the object "Object", which is an object of class "Class". Since Class inherits from Module, it inherits its instance method "const\_get", which can thus be called on any object of class "Class", i.e. any class.
> > 
> > If you don't really understand it, do not fear. It took me a while to get it too. Just remember that in Ruby everything is an object, and that includes classes themselves 🙂 . So you can send methods to them too.  
> > --  
> > Christophe Grandsire.
> > 
> > [http://rainbow.conlang.free.fr](http://rainbow.conlang.free.fr)
> > 
> > You need a straight mind to invent a twisted conlang.

---

<div class="post-metadata">

**Author:** ![Christophe\_Grandsire](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@Christophe\_Grandsire](https://rubytalk.org/u/Christophe_Grandsire)\
**Post date:** [14 October 2005 06:34 UTC](https://rubytalk.org/t/instance-from-class-name/21537/10 "2005-10-14T06:34:45Z")

</div>

Selon Alexander Lamb \<Alexander.J.Lamb@sim.hcuge.ch\>:

> Ah, I understand. Simply a class method (like in Objective-C).

That's how it comes out yes 🙂 . I guess I could have said it just that way, but  
then you'd still wonder why it was not in the documentation for Object 🙂 .

The thing to remember is that class methods are nothing more than instance  
methods on the class object (when one defines class methods, one is just adding  
singleton methods to the class object).

I just

> wasn't careful about the fact in inherited from Module (the class  
> object hierarchy). This can be difficult to understand for Java  
> developer who don't have real class objects and where static  
> functions are not inherited.

The small diagram in the documentation of Class (you can see it by running ri  
Class, or by looking in [RDoc Documentation](http://ruby-doc.org/core/) for the documentation of  
class Class) is a good thing to keep in mind. It shows how the class Object is  
an object of class Class, while at the same time Class inherits from Object via  
Module 😉 . I must admit it took me a while to get it 😉 . And I still sometimes  
get confused, which is why my explanation must have looked a bit confusing. I  
was trying to rebuild my knowledge of the thing at the same time I wrote 🙂 .

> **···**
>
> --  
> Christophe Grandsire.
> 
> [http://rainbow.conlang.free.fr](http://rainbow.conlang.free.fr)
> 
> It takes a straight mind to create a twisted conlang.
