I have a system at the moment where I create ‘things’ based on a base
class SceneObject, these ‘things’ have a Proc associated with them
that gets called to produce a different representation of the ‘thing’
(in my case data to be passed to a 3D renderer). At the moment it all
works like this…
class SceneObject
def initialize(&generate)
@generate = generate
end
def render
self.instance_eval(&@generate)
end
end
class MyObject < SceneObject
def initialize
super() do
# Render some objects
end
end
end
a = MyObject.new
a.render
This all works, but I’d like to ‘hide’ the class definition stuff, the
reason being that the data files passed through this system will be
defined outside, and don’t need to look like Ruby code, they 'use’
Ruby code to do the procedural stuff, but other than that I want them
to appear as simple as possible, so hiding classes, inheritance,
initialisation functions etc. would be a big benefit. Ideally I’d like
to do something like this, but can’t…
def defineClass(name,&generate)
eval <<-"end_eval"
class #{name} < SceneObject
def initialize
super(&#{generate})
end
end
end_eval
end
Then in my data files use this function like so…
defineClass(“MyObject”) do
Render some objects
end
a = MyObject.new
a.render
Can anyone offer any advice on how to achieve something like this?
Cheers
PaulG