El Sábado, 19 de Diciembre de 2009, Iñaki Baz Castillo escribió: > El Sábado, 19 de Diciembre de 2009, Roger Pack escribió: > > This confused me a bit > > > > class A > > @a = 2 > > @@a = 3 > > def self.a > > puts [@a, @@a] > > end > > end > > > > class B < A; end > > > > A.a # [2, 3] > > B.a # [nil, 3] > > > > shouldn't @a just lookup @@a in the parent? Different treatment for > > different types of variables? > > When you write: > > class A > @a = 2 > > take into account that @a=2 is just parsed when loading class A for first > time. > When you later do "class B < A; end", the Ruby interpreter doesn't read the > line "@a = 2" again as it already parsed class A and just stored in memory > the methods of class A. Take a look to this example which shows the same concept: irb> class A puts "I'm A" end I'm A nil irb> class B < A; end nil -- Iñaki Baz Castillo <ibc / aliax.net>