Hi everybody,
this is my first post and allready a nifty question
What I want:
Create a NAMED class at runtime. Adding functions is clear.
E.g. somthing like that: (ClassGenerator is an existing module)
module ClassGenerator
def self.generate_new_class ( Parent_Class_Name, Name )
def self.add_method( source )
end
somewhere else:
ClassGenerator::generate_new_class( Fixnum, MyInt );
ClassGenerator::MyInt.add_method( %{ def special_one
return 2
}%
)
Thanks,
David S.
On 8/3/06, David S. [email protected] wrote:
def self.generate_new_class ( Parent_Class_Name, Name )
Thanks,
David S.
â
Posted via http://www.ruby-forum.com/.
Something like the following should accomplish what you want:
module ClassGenerator
def self.generate_new_class(name, parent = Object)
const_set(name, Class.new(parent))
end
end
On 8/3/06, Gabe B. [email protected] wrote:
E.g. somthing like that: (ClassGenerator is an existing module)
}%
Something like the following should accomplish what you want:
module ClassGenerator
def self.generate_new_class(name, parent = Object)
const_set(name, Class.new(parent))
end
end
Sorry David, I should have added a usage example. ânameâ should be a
symbol:
ClassGenerator.generate_new_class(:MyInt, Fixnum)
On Aug 3, 2006, at 12:13 PM, David S. wrote:
def self.add_method( source )
Thanks,
David S.
â
Posted via http://www.ruby-forum.com/.
Object.const_set(âMyIntâ, Class.new(Fixnum))
David P. wrote:
David,
See http://dppruby.com/dppsrubyplayground/show/Domain+Specific+Languages
Read the preso. It explains (with code examples) how to dynamically
generate classes.
Thanks,
David
Thank you David
Perfect, i declare this thread as closed
David,
See http://dppruby.com/dppsrubyplayground/show/Domain+Specific+Languages
Read the preso. It explains (with code examples) how to dynamically
generate classes.
Thanks,
David
David S. wrote:
it works like this (just copy and paste)
module ClassGenerator
class Normal
def self.add_method(name, return_value )
define_method( name ) do
return return_value
end
end
def self.add_index( name, index )
define_method( name ) do
return index
end
define_method( 's_'+index.to_s ) do
return name
end
end
def self.foo
puts("Hello from foo")
end
end
def self.generate_new_class(name)#, parent = Class)
const_set(name, Class.new(Normal))
end
end
ClassGenerator.generate_new_class(:MyNew)
ClassGenerator::MyNew.foo
ClassGenerator::MyNew.add_method( âbarâ, âHello from Barâ)
s = ClassGenerator::MyNew.new
puts(s.bar)
ClassGenerator::MyNew.add_index(âwriteâ,1)
ClassGenerator::MyNew.add_index(âreadâ,2)
ClassGenerator::MyNew.add_index(âcreateâ,3)
puts( 'Write: '+s.write.to_s)
puts( 'Read: '+s.read.to_s)
puts( 'Create: '+s.create.to_s)
puts( '1: '+s.s_1)
puts( '2: '+s.s_2)
puts( '3: '+s.s_3)
On Fri, 4 Aug 2006, David S. wrote:
David S. wrote:
it works like this (just copy and paste)
i thought iâd chime in here and point out that the idea of this is
flawed:
if you know the name of the class you want to generate, letâs say
âMyClassâ,
and you also want to refer to it via the bareword constant MyClass then
you
can simply do
class MyClass; end
and later
class MyClass
def an_added_method
# âŠ
end
end
the point being that, unless you are going to also going retrieve
you
classes via the string name, as in
c = const_get âMyClassâ
obj = c::new
not
c = create_class âMyClassâ
obj = MyClass.new
then you do not need to dynamically create classes by name since the
name is
know apriori
perhaps i misunderstodd the OP, but the question did suggest a flawed
design.
regards.
-a
On Fri, 4 Aug 2006, Robert D. wrote:
his reasons )
I had a quick look into class.c and it seems that rb_define_class is the
only way to define the name of the class object, which would mean that eval
is the only way to do the trick.
Any opinnions?
i think weâve come full circle, but you can do
c = Object.const_set âCâ, Class.new{}
i was just unsure it was actually needed
cheers.
-a
On 8/3/06, [email protected] [email protected] wrote:
if you know the name of the class you want to generate, letâs say
def an_added_method
not
c = create_class âMyClassâ
obj = MyClass.new
then you do not need to dynamically create classes by name since the name
is
know apriori
Hmm but that still leaves the OPâs question unanswered, how can I
create
a named class dynamically.
eval âclass #{x}; endâ
myref2class = Object.const_get(x)
obviously does the job. ( I agree seems flawed to me too, but OP might
have
his reasons )
I had a quick look into class.c and it seems that rb_define_class is the
only way to define the name of the class object, which would mean that
eval
is the only way to do the trick.
Any opinnions?
Cheers
Robert
we can never obtain peace in the outer world until we make peace with
ourselves.
Hmmm your quotes enlighten me (even more than your posts
On 8/3/06, [email protected] [email protected] wrote:
myref2class = Object.const_get(x)
Any opinnions?
i think weâve come full circle, but you can do
c = Object.const_set âCâ, Class.new{}
i was just unsure it was actually needed
Thx I scr**** up on this one, thaught I had tested it. Defenitly nicer
than
eval.
Well I guess that is what OP asked for, which does not necessairily mean
it
is what he wanted.
or even less what he needed.
But it is what I wanted to know
Thx again.
Cheers
Robert
cheers.
-a
happiness is not something ready-made. it comes from your own actions.
â
Deux choses sont infinies : lâunivers et la bĂÂȘtise humaine ; en ce qui
concerne lâunivers, je nâen ai pas acquis la certitude absolue.