Creating Classes at runtime

Hi everybody,
this is my first post and allready a nifty question :wink:

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

  • Gabe

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

  • Gabe

Sorry David, I should have added a usage example. ‘name’ should be a
symbol:

ClassGenerator.generate_new_class(:MyInt, Fixnum)

  • Gabe

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 :wink:
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.

  • h.h. the 14th dali lama

Hmmm your quotes enlighten me (even more than your posts :wink:

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. :frowning:
or even less what he needed.

But it is what I wanted :slight_smile: to know
Thx again.
Cheers
Robert

cheers.

-a

happiness is not something ready-made. it comes from your own actions.

  • h.h. the 14th dali lama

–
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.

  • Albert Einstein