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 Stokar

路路路

--
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, David Stokar <stodavid@student.ethz.ch> wrote:

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 Stokar

--
Posted via http://www.ruby-forum.com/\.

Object.const_set("MyInt", Class.new(Fixnum))

路路路

On Aug 3, 2006, at 12:13 PM, David Stokar wrote:

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 Stokar

--
Posted via http://www.ruby-forum.com/\.

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

路路路

On 8/3/06, David Stokar <stodavid@student.ethz.ch> wrote:

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 Stokar

--
Posted via http://www.ruby-forum.com/\.

--
--------
David Pollak's Ruby Playground
http://dppruby.com

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

ClassGenerator.generate_new_class(:MyInt, Fixnum)

- Gabe

路路路

On 8/3/06, Gabe Boyer <gboyer@gmail.com> wrote:

On 8/3/06, David Stokar <stodavid@student.ethz.ch> wrote:
>
> 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 Stokar
>
> --
> 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

David Pollak 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

路路路

--
Posted via http://www.ruby-forum.com/\.

David Stokar 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)

路路路

--
Posted via http://www.ruby-forum.com/.

<snip positive impl>

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, David Stokar wrote:

David Stokar wrote:
it works like this (just copy and paste)

--
we can never obtain peace in the outer world until we make peace with
ourselves.
- h.h. the 14th dali lama

> David Stokar wrote:
> it works like this (just copy and paste)

<snip positive impl>

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

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

<snip>

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:

<cut my old quote>

路路路

On 8/3/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Fri, 4 Aug 2006, David Stokar wrote:

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 Fri, 4 Aug 2006, Robert Dober wrote:

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?

--
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama

>
> 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?

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.

路路路

On 8/3/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Fri, 4 Aug 2006, Robert Dober wrote:

-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