Forum: Ruby How can I create a ruby class in runtime

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
14b7344777a56ac9a1fad849f5d6da2a?d=identicon&s=25 unknown (Guest)
on 2005-12-23 21:25
(Received via mailing list)
Hi,

Does anyone know how to create a ruby class in runtime?

ex:
class Parent
   def...
   def...
end

in runtime, we want to create a class "child" inherited from "parent",
it is possible?

thanks you very much

sayoyo
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 =?ISO-8859-1?Q?Florian_Gro=DF?= (Guest)
on 2005-12-23 21:31
(Received via mailing list)
sayoyo@yahoo.com wrote:

> in runtime, we want to create a class "child" inherited from "parent",
> it is possible?

Yup, it is:

child = Class.new(parent) do
   def method(arg)
     ...
   end

   define_method(dynamic_name) do |arg|
     ...
   end

   # There's a gotcha, though:
   # Constant = 5 doesn't work as expected. Use this instead:
   const_set(:Constant, 5)
end
D84df7c68f790e492c4ad4ec5fe65547?d=identicon&s=25 Florian Frank (Guest)
on 2005-12-23 21:34
(Received via mailing list)
sayoyo@yahoo.com wrote:

>in runtime, we want to create a class "child" inherited from "parent",
>it is possible?
>
>
klass = Class.new Parent
This topic is locked and can not be replied to.