How can I create a ruby class in runtime

S

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
 
F

Florian Groß

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
 

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
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top