How can I create a ruby class in runtime

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I create a ruby class in runtime

sayoyo Sayoyo
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


Reply | Threaded
Open this post in threaded view
|

Re: How can I create a ruby class in runtime

Florian Gross
[hidden email] 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

--
http://flgr.0x42.net/


Reply | Threaded
Open this post in threaded view
|

Re: How can I create a ruby class in runtime

Florian Frank
In reply to this post by sayoyo Sayoyo
[hidden email] wrote:

>in runtime, we want to create a class "child" inherited from "parent",
>it is possible?
>  
>
klass = Class.new Parent

--
Florian Frank