Robert Klemme
6/14/2008 6:15:00 PM
On 14.06.2008 19:38, Phlip wrote:
> Sebastian Hungerecker wrote:
>
>> Rob Boellaard wrote:
>
>>> If I have class A and class B, and they both inherit from S.
>>> And in S there is a class method called doSomething.
>
>> self will be A or B.
>
> self.class.name will == 'A' or 'B'
No, self.name will be "A" or "B" - self.class.name is "Class".
Remember, we are talking about a class method.
irb(main):001:0> class Base
irb(main):002:1> def self.x
irb(main):003:2> [self.class.name, self.name]
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> class A<Base;end
=> nil
irb(main):007:0> class B<Base;end
=> nil
irb(main):008:0> A.x
=> ["Class", "A"]
irb(main):009:0> B.x
=> ["Class", "B"]
irb(main):010:0>
I would not use the name but rather self (the class object) itself if I
would want to do some class specific stuff, but:
> Tip: Using this information, such as in an 'if' statement, leads to bad
> design. The purpose of OO programming is polymorphism. A and B should have
> derived methods that doSomething calls. That's the Abstract Template
> pattern.
Absolutely agree.
Kind regards
robert