special cased?

R

Roger Pack

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?

-r
 
I

Iñaki Baz Castillo

El S=C3=A1bado, 19 de Diciembre de 2009, Roger Pack escribi=C3=B3:
This confused me a bit
=20
class A
@a =3D 2
@@a =3D 3
def self.a
puts [@a, @@a]
end
end
=20
class B < A; end
=20
A.a # [2, 3]
B.a # [nil, 3]
=20
shouldn't @a just lookup @@a in the parent? Different treatment for
different types of variables?

When you write:

class A
@a =3D 2

take into account that @a=3D2 is just parsed when loading class A for first=
=20
time.
When you later do "class B < A; end", the Ruby interpreter doesn't read the=
=20
line "@a =3D 2" again as it already parsed class A and just stored in memor=
y the=20
methods of class A.



=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
I

Iñaki Baz Castillo

El S=C3=A1bado, 19 de Diciembre de 2009, I=C3=B1aki Baz Castillo escribi=C3=
=B3:
El S=C3=A1bado, 19 de Diciembre de 2009, Roger Pack escribi=C3=B3:
This confused me a bit

class A
@a =3D 2
@@a =3D 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?
=20
When you write:
=20
class A
@a =3D 2
=20
take into account that @a=3D2 is just parsed when loading class A for fir= st
time.
When you later do "class B < A; end", the Ruby interpreter doesn't read t= he
line "@a =3D 2" again as it already parsed class A and just stored in mem= ory
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


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
B

Brian Candler

Roger said:
shouldn't @a just lookup @@a in the parent?

Nope - class variables (@@) have nothing to do with instance variables
(@). @a in this case is just an instance variable of the class object,
so @a in class B is different to @a in class A

Class variables are a very strange beast with bizarre semantics. I would
avoid them if I were you.
 
D

David A. Black

Hi --

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?

I think you mean shouldn't it just look up @a (not @@a) in the parent
-- and the answer is no :) A and B are different objects, and
therefore do not share instance variables.


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
 
I

Iñaki Baz Castillo

El S=E1bado, 19 de Diciembre de 2009, David A. Black escribi=F3:
I think you mean shouldn't it just look up @a (not @@a) in the parent
-- and the answer is no :) A and B are different objects, and
therefore do not share instance variables.

class A
@a =3D "hello"
=20
def self.say
puts defined?(@a).inspect
end
end

A.say
=3D> "instance-variable"
nil

class B < A ; end

B.say
nil
nil


But as I already explained, the line '@a =3D "hello"' is just parsed by Rub=
y=20
interpreter when loading class A.

When creating class B (which inherits from A) the Ruby parser doesn't read =
the=20
whole class A definition again. Instead it already has in memory all the=20
class/instance methods defined for class A so it wouldn't read '@a =3D "hel=
lo"'=20
anymore.

And because of it, B class object doesn't know @a as its instance variable,=
it=20
knows nothing about @a.

=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
R

Roger Pack

Class variables are a very strange beast with bizarre semantics. I would
avoid them if I were you.

Yeah they must be special cased so that beginning users can use them
with abandon and it will "just work."

Interesting.

in
class A
@@a = 3
end

class B < A
def go
@@a
end
end

in B.new.go => 3...where is @@a stored?...it's not in B...it's not in
A's nearest ancestor, which is "Object"...it's special cased somehow?

Roughly paraphrasing a quote I heard once...
"after using ruby for 5 years, it is still surprising to me" (no offence
intended, of course :)
Cheers.
-r
 
B

Brian Candler

Roger said:
class A
@@a = 3
end

class B < A
def go
@@a
end
end

in B.new.go => 3...where is @@a stored?...it's not in B...it's not in
A's nearest ancestor, which is "Object"...it's special cased somehow?

I believe it's picking up the value from class A, since that's B's
ancestor, and B doesn't already have an @@a of its own.

It gets scarier though. Have a look at this:
NameError: uninitialized class variable @@a in A
from (irb):3:in `a'
from (irb):23
from :0NameError: uninitialized class variable @@a in A
from (irb):3:in `a'
from (irb):24
from :0=> 1

So which 'version' of the @@a class variable you see, depends on where
the method which reads it was defined... or something like that.
 
D

David A. Black

Hi --

Yeah they must be special cased so that beginning users can use them
with abandon and it will "just work."

See this, from Matz:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/347355

especially:

(3) and lastly, and most importantly, do not use class variables,
unless you really really need them; they are fundamentally global
variables.

That's the thing; they're really hierarchy globals, but confusingly
similar to instance variables (of which they are essentially the
opposite) in appearance.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top