irb(main):001:0> class A; def initialize; puts 'in A'; end; end
=> nil
irb(main):002:0> a = eval("A.new")
in A
=> #<A:0x2ad3400>
irb(main):003:0> a = Module.const_get("A").new
in A
=> #<A:0x2ad02c8>
Person = Class.new
or
Object.const_set "Person", Class.new
Both will create a new class named "Person". The second method will
let you use a name determined at runtime. If I understand your Java
code above, the Ruby translation would be something like this:
klass = Object.const_set("Person", Class.new)
person = klass.new
HTH,
Mark
···
On 5/15/05, baalbek <rcs@bgoark.no> wrote:
In Java there is a construct like this:
Class clazz = Class.forName("Person");
Person person = (Person)clazz.newInstance();
Is there something similar available for Ruby, that is, can I create a
class on the fly only having this class' name as a string?
In other words, something like this (for Ruby):
person = Class.new("Person")
This does not work, but is there something that would work like this?