From: "David A. Black" Date: 2009-12-20T04:16:56+09:00 Subject: Re: special cased? --1926193751-1797597716-1261250204=:32247 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1797597716-1261250204=:32247" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1797597716-1261250204=:32247 Content-Type: TEXT/PLAIN; charset=utf-8; format=flowed Content-Transfer-Encoding: 8BIT Hi -- On Sat, 19 Dec 2009, I単aki Baz Castillo wrote: > 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. Also, instance variables are always pegged to "self". class A puts self # A @x = 1 end class B < A puts self # B puts @x # nil end Every object, including every Class object, has its own supply of instance variable "slots", and the meaning of @var is always self.instance_variable_get("@var"). David -- David A. Black Senior Developer, Cyrus Innovation Inc. THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally! January 22-23, Tampa, Florida Info and registration at http://www.thecompleatrubyist.com --1926193751-1797597716-1261250204=:32247-- --1926193751-1797597716-1261250204=:32247--