I'm a Java programmer that is trying to learn Ruby.
I'm trying to write a container that creates objects on the fly from a descriptor. In Java I'd simply do Class.forName("MyClass").newInstance();
Is there an easy way to do this in Ruby? I basically want a loader that I can call Loader.createObject(filename, classname) where filename is the name of the ruby file containing the class and classname is the actual class.
Also, since I'll be creating more than one instance of each class, how do I store the class instance?
I'm a Java programmer that is trying to learn Ruby.
I'm trying to write a container that creates objects on the fly from a descriptor. In Java I'd simply do Class.forName("MyClass").newInstance();
mc = Object.const_get( "MyClass" ).new
This presumes that the code already knows about the class MyClass (e.g., because the class is defined in the same file, or defined in a file that has already been read using 'require' or 'load').
BTW, I looked at the Ruby FAQ, and this question doesn't seem to be in there, despite its popularity.
I'm a Java programmer that is trying to learn Ruby.
I'm trying to write a container that creates objects on the fly from a
descriptor. In Java I'd simply do
Class.forName("MyClass").newInstance();
One way is like:
Object.const_get("MyClass").new # returns an instance of MyClass
Or, if your classes are inside a module (say, "MyModule"), then
MyModule.const_get("MyClass").new
Is there an easy way to do this in Ruby? I basically want a loader
that I can call Loader.createObject(filename, classname) where filename
is the name of the ruby file containing the class and classname is the
actual class.
Also, since I'll be creating more than one instance of each class, how
do I store the class instance?
klass = Object.const_get("MyClass") # Returns the class MyClass
obj = klass.new # Returns an instance of MyClass
There are probably other ways to do these things, but this is the one
I'm familiar with.
···
On 2005-06-05, Bill <ruby@thebackfamily.com> wrote:
I'm a Java programmer that is trying to learn Ruby.
I'm trying to write a container that creates objects on the fly from a
descriptor. In Java I'd simply do
Class.forName("MyClass").newInstance();
Is there an easy way to do this in Ruby? I basically want a loader
that I can call Loader.createObject(filename, classname) where filename
is the name of the ruby file containing the class and classname is the
actual class.
Also, since I'll be creating more than one instance of each class, how
do I store the class instance?
You can always eval: obj = eval(classname).new, but this is cleaner:
cls = const_get classname
obj = cls.new
If you know the class is in some namespace (or other module):
cls = ModuleName.const_get classname
TIA.
Bill.
E
···
Le 5/6/2005, "Bill" <ruby@thebackfamily.com> a écrit:
Thanks for all the quick help. The presumption was my problem. I had some code that I got that should have worked, but wasn't requiring the file. I'm now able to create classes from external files/modules with the following.
On 2005-06-05 09:51:42 -0400, James Britt <james_b@neurogami.com> said:
Bill wrote:
Hi,
I'm a Java programmer that is trying to learn Ruby.
I'm trying to write a container that creates objects on the fly from a descriptor. In Java I'd simply do Class.forName("MyClass").newInstance();
mc = Object.const_get( "MyClass" ).new
This presumes that the code already knows about the class MyClass (e.g., because the class is defined in the same file, or defined in a file that has already been read using 'require' or 'load').
BTW, I looked at the Ruby FAQ, and this question doesn't seem to be in there, despite its popularity.
On 2005-06-05 09:51:42 -0400, James Britt <james_b@neurogami.com> said:
Bill wrote:
Hi,
I'm a Java programmer that is trying to learn Ruby.
I'm trying to write a container that creates objects on the fly from a descriptor. In Java I'd simply do Class.forName("MyClass").newInstance();
mc = Object.const_get( "MyClass" ).new
This presumes that the code already knows about the class MyClass (e.g., because the class is defined in the same file, or defined in a file that has already been read using 'require' or 'load').
BTW, I looked at the Ruby FAQ, and this question doesn't seem to be in there, despite its popularity.
Thanks for all the quick help. The presumption was my problem. I had some code that I got that should have worked, but wasn't requiring the file. I'm now able to create classes from external files/modules with the following.