From: Chochain Lee Date: 2001-05-04T10:09:43+09:00 Subject: [ruby-talk:14609] scope of base class instance variable This is a multi-part message in MIME format. ------=_NextPart_000_0019_01C0D3FB.F9799240 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I have done OO Perl for a while and started Ruby recently. I have had = fun but run into some fundamental question and can't find info regarding = the scope of base class instance variable in David & Andrew's book. The following code #------------------------------------------------------------- class Base def initialize() @s =3D "Base::init@#{self.class}" @v =3D 1 puts "<#{@s.id}>=3D#{@s}, <#{@v.id}>=3D#{@v}" end def dump_base puts "<#{@s.id}>=3D#{@s}, <#{@v.id}>=3D#{@v}" end end class Ext < Base def initialize() super @s =3D "Ext::init@#{self.class}" @v =3D 2 end def dump_ext=20 puts "<#{@s.id}>=3D#{@s}, <#{@v.id}>=3D#{@v}" end end $y =3D Ext.new() $y.dump_ext() $y.dump_base() #------------------------------------------------------------ gives me the following result which is kind of unexpected <84004400>=3DBase::init@Ext, <3>=3D1 <84004412>=3DExt::init@Ext, <5>=3D2 <84004412>=3DExt::init@Ext, <5>=3D2 Is this by Ruby's design? If that is the case, how can I prevent myself = from overwriting the variables if I use someone else's library without = peeking into their source? Your answer is much appreciated. /\ /\ Chochain Lee = --------------------QQQ-^--QQQ------------------------- ------=_NextPart_000_0019_01C0D3FB.F9799240 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hi,
 
I have done OO Perl for a while and = started Ruby=20 recently. I have had fun but run into some fundamental question and = can't=20 find info regarding the scope of base class instance = variable in David=20 & Andrew's book.
 
The following code
#---------------------------------------------------------= ----
class Base
  def=20 initialize()
    @s =3D=20 "Base::init@#{self.class}"
    @v =3D=20 1
    puts "<
=3D#{@s">#{@s.id}>=3D#{@s}, = <=3D#{@v">#{@v.id}>=3D#{@v}"
 =20 end
  def dump_base
    puts = "<
=3D#{@s">#{@s.id}>=3D#{@s}, = <=3D#{@v">#{@v.id}>=3D#{@v}"
 =20 end
end
 
class Ext < Base
  def=20 initialize()
    super
    @s = =3D=20 "Ext::init@#{self.class}"
    @v =3D 2
 =20 end
  def dump_ext 
    puts = "<
=3D#{@s">#{@s.id}>=3D#{@s}, = <=3D#{@v">#{@v.id}>=3D#{@v}"
 =20 end
end
 
$y =3D=20 Ext.new()
$y.dump_ext()
$y.dump_base()
#------------------------= ------------------------------------
 
gives me the following result which is kind of=20 unexpected
 
<84004400>=3DBase::init@Ext,=20 <3>=3D1
<84004412>=3DExt::init@Ext,=20 <5>=3D2
<84004412>=3DExt::init@Ext, = <5>=3D2
Is this by Ruby's design? If that is the = case,  how=20 can I prevent myself from overwriting the variables if I use = someone=20 else's library without peeking into their source? Your answer is much=20 appreciated.
         &nbs= p;            = ;            =  =20 /\    /\
Chochain Lee     =20 --------------------QQQ-^--QQQ-------------------------
<= /BODY> ------=_NextPart_000_0019_01C0D3FB.F9799240--