[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Classes at runtime

Lethalman

2/7/2005 9:40:00 AM

(sorry for my poor English)
Hello,
i can make classes at runtime in Python, just like this:

class test: pass
class myclass:
def make(self, inherit):
class temp(inherit): pass
return temp()

instance = myclass().make(test)

instance is now a class which inherits test... this is a really simple
example but i need to know because i can't do this in ruby.
So, is python more dynamic with objects? Isn't ruby good for this kind
of meta programming?
2 Answers

mark sparshatt

2/7/2005 10:06:00 AM

0

Lethalman wrote:
> (sorry for my poor English)
> Hello,
> i can make classes at runtime in Python, just like this:
>
> class test: pass
> class myclass:
> def make(self, inherit):
> class temp(inherit): pass
> return temp()
>
> instance = myclass().make(test)
>
> instance is now a class which inherits test... this is a really simple
> example but i need to know because i can't do this in ruby.
> So, is python more dynamic with objects? Isn't ruby good for this kind
> of meta programming?
>
>

Try this

class Test
end

klass = Class.new(Test)
p klass.ancestors #=> [#<Class:.....>, Test, Object, Kernel]

klass is a subclass of Test.

HTH

--
Mark Sparshatt




gabriele renzi

2/7/2005 5:11:00 PM

0

mark sparshatt ha scritto:

>> So, is python more dynamic with objects? Isn't ruby good for this kind
>> of meta programming?
>>
>>
>
> Try this
>
> class Test
> end
>
> klass = Class.new(Test)
> p klass.ancestors #=> [#<Class:.....>, Test, Object, Kernel]
>
> klass is a subclass of Test.
>
> HTH

... and consider you can normally just use a mixin in ruby
>> module Foo
>> def foo
>> 'mixins rule!'
>> end
>> end
=> nil
>> a='hello'
=> "hello"
>> a.extend Foo
=> "hello"
>> a.foo
=> "mixins rule!"
>> a.is_a? Foo
=> true