<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>PaulBarry.com - Instantiating a object from a string</title>
  <subtitle type="html">My thoughts, ideas, questions and concerns on technology, sports, music and life</subtitle>
  <id>tag:paulbarry.com,2007:Paulbarry.com</id>
  <generator uri="http://www.paulbarry.com" version="3.0">PaulBarry.com</generator>
  <link href="http://paulbarry.com/xml/atom/article/4818/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://paulbarry.com/articles/2007/08/28/instantiating-a-object-from-a-string" rel="alternate" type="text/html"/>

  <updated>2008-11-06T13:34:42-05:00</updated>
  <entry>
    <author>
      <name>Paul Barry</name>
      <email>mail@paulbarry.com</email>
    </author>
    <id>urn:uuid:ca7c9a78-b124-4d9c-8b1f-c0a10dec97f0</id>

    <published>2007-08-28T20:01:05-04:00</published>
    <updated>2007-08-28T20:01:05-04:00</updated>
    <title type="html">Instantiating a object from a string</title>
    <link href="http://paulbarry.com/articles/2007/08/28/instantiating-a-object-from-a-string" rel="alternate" type="text/html"/>

    <category term="technology" scheme="http://paulbarry.com/articles/category/technology" label="Technology"/>
        <category term="Rails" scheme="http://paulbarry.com/articles/tag/rails"/>
    <category term="Javascript" scheme="http://paulbarry.com/articles/tag/javascript"/>
    <category term="Ruby" scheme="http://paulbarry.com/articles/tag/ruby"/>
        <summary type="html">&lt;p&gt;Today, while doing some Ruby programming, I needed to create an instance of a class (instantiate an object), but I didn&apos;t have the class name, just the class name in a string.  Here&apos;s a Ruby method that takes the name of the class as a string and returns an instance of that class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def create(class_name)
  Object.const_get(class_name).new
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So if you do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;create &quot;Array&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You get an instance of an Array, in other words, the equivalent of &lt;code&gt;Array.new&lt;/code&gt;.  And in Rails, you can do:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def create(class_name)
  class_name.constantize.new
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&apos;s cool.  It got me thinking, how do you do this in other languages?  Here&apos;s what I came up with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;//JavaScript
function create(class_name) {
    return eval(&quot;new &quot;+class_name+&quot;()&quot;)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Got any others? Perl? Python? Lisp? Erlang? PHP? C? Java?&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Today, while doing some Ruby programming, I needed to create an instance of a class (instantiate an object), but I didn&apos;t have the class name, just the class name in a string.  Here&apos;s a Ruby method that takes the name of the class as a string and returns an instance of that class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def create(class_name)
  Object.const_get(class_name).new
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So if you do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;create &quot;Array&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You get an instance of an Array, in other words, the equivalent of &lt;code&gt;Array.new&lt;/code&gt;.  And in Rails, you can do:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def create(class_name)
  class_name.constantize.new
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&apos;s cool.  It got me thinking, how do you do this in other languages?  Here&apos;s what I came up with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;//JavaScript
function create(class_name) {
    return eval(&quot;new &quot;+class_name+&quot;()&quot;)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Got any others? Perl? Python? Lisp? Erlang? PHP? C? Java?&lt;/p&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>Arni Hermann Reynisson</name>
    </author>
    <id>urn:uuid:069de957-a159-43b0-a37b-5f174145e354</id>
    <published>2007-09-04T18:15:23-04:00</published>
    <updated>2007-09-04T18:15:23-04:00</updated>
    <title type="html">Comment on "Instantiating a object from a string" by Arni Hermann Reynisson</title>
    <link href="http://paulbarry.com/articles/2007/08/28/instantiating-a-object-from-a-string#comment-5064" rel="alternate" type="text/html"/>
    <content type="html">In my experience you can almost always avoid eval() in javascript so here are my 2 cents:&lt;br/&gt;&lt;br/&gt;function create(className) {&lt;br/&gt;  return new this[className];&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;There are, of course, other ways of doing that depending on what you need...</content>
  </entry>
  </feed>