[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Creating Classes at runtime

David Stokar

8/3/2006 4:13:00 PM

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 Stokar

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

6 Answers

Logan Capaldo

8/3/2006 4:51:00 PM

0


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

> 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 Stokar
>
> --
> Posted via http://www.ruby-....
>

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


David Pollak

8/3/2006 4:52:00 PM

0

David,

See http://d.../dppsrubyplayground/show/Domain+Specific...

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 ;-)
>
> 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-....
>
>


--
--------
David Pollak's Ruby Playground
http://d...

David Stokar

8/3/2006 5:14:00 PM

0

David Pollak wrote:
> David,
>
> See http://dppruby.com/dppsrubyplayground/show/Domain+Specific...
>
> 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

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

David Stokar

8/3/2006 5:55:00 PM

0

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

Ara.T.Howard

8/3/2006 6:06:00 PM

0

Ara.T.Howard

8/3/2006 9:21:00 PM

0