Dynamic Class Instance?

K

Kale Davis

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
 
M

Marnen Laibow-Koser

Kale said:
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
(e-mail address removed)
 
B

Ben Bleything

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/

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
 
R

Robert Klemme

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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: http://gist.github.com/294520

( mirrored at http://pastie.org/private/1gtz2apwaxac2rcgl3xsw because github
is having issues 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.
 
R

Robert Klemme

[Note: parts of this message were removed to make it a legal post.]

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: http://gist.github.com/294520

( mirrored at http://pastie.org/private/1gtz2apwaxac2rcgl3xsw because github
is having issues 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.

That's exactly what I had in mind. Thanks for fleshing it out!

Kind regards

robert
 
A

Albert Schlef

Josh said:
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: 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 Bleything, 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top