0x22

Your daily class variables and constants surprise

Jul 20, 2009

It is not chrismas, hence no quiz, but this was strangely surprising:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class A
  @@t = "A::@@t"
  T="A::T"

  def self.s1; @@t; end
  def self.s2; T; end
end

class B < A
  def self.s1; @@t; end
  def self.s2; T; end
end

class C < A
  @@t = "C::@@t"
  T="C::T"
  def self.s1; @@t; end
  def self.s2; T; end
end

[ A.s1, A.s2, B.s1, B.s2, C.s1, C.s2 ]

gives you

1
["C::@@t", "A::T", "C::@@t", "A::T", "C::@@t", "C::T"]
Toggle Comments