# Dynamic Class Instance?

**URL:** <https://www.ruby-forum.com/t/dynamic-class-instance/182846>\
**Category:** Ruby\
**Created:** [February 3, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846 "2010-02-03T20:09:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@kale](https://www.ruby-forum.com/u/kale)\
**Post date:** [February 3, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846/1 "2010-02-03T20:09:25Z")

</div>

I am creating a Ruby script that basically:

1. Has a prompt that gets a command string (ex. ‘edit user1 15’)
2. Parses the string
3. Runs specific command (like ‘edit’ or ‘new’)
4. Repeats

From a functional level I know how to make this work, but how do I make  
it dynamic in the sense that another user could easily add their own  
commands by adding a new subclass?

I was thinking something like this:

class Update \< Command  
…  
end

The problem is I donâ€™t know how to dynamically create a new ‘Update’  
instance if that is the command string (ie ‘update user’)? I know how to  
call a method using ‘send’ and I guess that would be another approach  
maybe, but is there something for classes?

cmd = gets.chomp #ie ‘update user’  
c = cmd.split(’ ')[0].new #simple parsing example  
c.run

Thanks for any help!  
Kale

---

<div class="post-metadata">

**Author:** ![kale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@kale](https://www.ruby-forum.com/u/kale)\
**Post date:** [February 3, 2010, 8:13pm UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846/2 "2010-02-03T20:13:11Z")

</div>

Kale D. wrote:

> I am creating a Ruby script that basically:
> 
> 1. Has a prompt that gets a command string (ex. ‘edit user1 15’)
> 2. Parses the string
> 3. Runs specific command (like ‘edit’ or ‘new’)
> 4. Repeats
> 
> From a functional level I know how to make this work, but how do I make  
> it dynamic in the sense that another user could easily add their own  
> commands by adding a new subclass?
> 
> I was thinking something like this:
> 
> class Update \< Command  
> …  
> end
> 
> The problem is I donâ€™t know how to dynamically create a new ‘Update’  
> instance if that is the command string (ie ‘update user’)? I know how to  
> call a method using ‘send’ and I guess that would be another approach  
> maybe, but is there something for classes?

Classes are objects too. They respond to Object#send.

> cmd = gets.chomp #ie ‘update user’  
> c = cmd.split(’ ')[0].new #simple parsing example  
> c.run
> 
> Thanks for any help!  
> Kale

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@kale](https://www.ruby-forum.com/u/kale)\
**Post date:** [February 3, 2010, 8:28pm UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846/3 "2010-02-03T20:28:23Z")

</div>

On Wed, Feb 3, 2010 at 11:10 AM, Kale D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am creating a Ruby script that basically:
> 
> 1. Has a prompt that gets a command string (ex. ‘edit user1 15’)
> 2. Parses the string
> 3. Runs specific command (like ‘edit’ or ‘new’)
> 4. Repeats
> 
> From a functional level I know how to make this work, but how do I make  
> it dynamic in the sense that another user could easily add their own  
> commands by adding a new subclass?

You may be interested in a somewhat ancient project of mine called  
Linen which does what you’re describing:  
[http://bitbucket.org/laika/linen/src/](http://bitbucket.org/laika/linen/src/)

I haven’t touched that code for years, and it had some issues when I  
last worked with it, so I’m not really suggesting you use it, so much  
as look at it for ideas of how to do what you want 🙂

Ben

---

<div class="post-metadata">

**Author:** ![kale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@kale](https://www.ruby-forum.com/u/kale)\
**Post date:** [February 4, 2010, 12:03pm UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846/4 "2010-02-04T12:03:46Z")

</div>

Hi, not completely sure I understand what you are trying to do, but  
Robert’s  
post got me interested, and I wanted to try it out.  
This is what I came up with: [294520’s gists · GitHub](http://gist.github.com/294520)

( mirrored at [http://pastie.org/private/1gtz2apwaxac2rcgl3xsw](http://pastie.org/private/1gtz2apwaxac2rcgl3xsw) because  
github  
is having issues [http://twitter.com/github/status/8627608544](http://twitter.com/github/status/8627608544) )

Hopefully it’s helpful, if not, maybe flesh out the example a bit more  
of  
what you want to do, and what you want other people to be able to do.

---

<div class="post-metadata">

**Author:** ![kale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@kale](https://www.ruby-forum.com/u/kale)\
**Post date:** [February 4, 2010, 12:24pm UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846/5 "2010-02-04T12:24:50Z")

</div>

On 02/04/2010 12:03 PM, Josh C. wrote:

> what you want to do, and what you want other people to be able to do.  
> That’s exactly what I had in mind. Thanks for fleshing it out!

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@kale](https://www.ruby-forum.com/u/kale)\
**Post date:** [February 4, 2010, 9:16am UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846/6 "2010-02-04T09:16:22Z")

</div>

On 03.02.2010 20:13, Marnen Laibow-Koser wrote:

> > 
> 
> Classes are objects too. They respond to Object#send.

For that he first needs the class instance. Something like cl =  
Object.const\_get(“Update”) will do. Then you can do whatever you want  
to do with it. In this case all classes should probably share a common  
interface method, e.g.

class Command  
def execute(\*args)  
raise “Not implemented”  
end  
end

class Update \< Command  
def execute(\*a)  
…  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/91b2a8/32.png) [@kale](https://www.ruby-forum.com/u/kale)\
**Post date:** [February 4, 2010, 11:30pm UTC](https://www.ruby-forum.com/t/dynamic-class-instance/182846/7 "2010-02-04T23:30:26Z")

</div>

Josh C. wrote:

> Hi, not completely sure I understand what you are trying to do, but  
> Robert’s post got me interested, and I wanted to try it out.  
> This is what I came up with: [294520’s gists · GitHub](http://gist.github.com/294520)
> 
> module Command  
> […]  
> def self.included( base )  
> @registered\_commands \<\< base  
> end

And when using a base class instead of a module, one could use the  
Class#inherited hook (that’s the route Ben B., above, took in  
‘linen’).

> cmd\_class = class\_name.classify.constantize

Better let each command tell us its name, or names. It allows for  
several names (aliases) for a command.
