This is a multi-part message in MIME format.
------ extPart_000_0019_01C0D3FB.F9799240
Content-Type: text/plain;
charset so-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 = "Base::init@#{self.class}"
@v = 1
puts "<#{@s.id}>=#{@s}, <#{@v.id}>=#{@v}"
end
def dump_base
puts "<#{@s.id}>=#{@s}, <#{@v.id}>=#{@v}"
end
end
class Ext < Base
def initialize()
super
@s = "Ext::init@#{self.class}"
@v = 2
end
def dump_ext
puts "<#{@s.id}>=#{@s}, <#{@v.id}>=#{@v}"
end
end
$y = Ext.new()
$y.dump_ext()
$y.dump_base()
#------------------------------------------------------------
gives me the following result which is kind of unexpected
<84004400>=Base::init@Ext, <3>=1
<84004412>=Ext::init@Ext, <5>=2
<84004412>=Ext::init@Ext, <5>=2
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-------------------------
------ extPart_000_0019_01C0D3FB.F9799240
Content-Type: text/html;
charset so-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4522.1800" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial>Hi,</FONT></DIV>
<DIV><FONT face=Arial></FONT> </DIV>
<DIV><FONT face=Arial>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.</FONT></DIV>
<DIV><FONT face=Arial></FONT> </DIV>
<DIV><FONT face=Arial>The following code</FONT></DIV>
<DIV><FONT
face=Courier>#-------------------------------------------------------------</FONT></DIV>
<DIV><FONT face=Courier>class Base<BR> def
initialize()<BR> @s =
"Base::init@#{self.class}"<BR> @v =
1<BR> puts "<</FONT><A
href="mailto:#{@s.id}>=#{@s"><FONT
face=Courier>#{@s.id}>=#{@s</FONT></A><FONT face=Courier>}, <</FONT><A
href="mailto:#{@v.id}>=#{@v"><FONT
face=Courier>#{@v.id}>=#{@v</FONT></A><FONT face=Courier>}"<BR>
end<BR> def dump_base<BR> puts "<</FONT><A
href="mailto:#{@s.id}>=#{@s"><FONT
face=Courier>#{@s.id}>=#{@s</FONT></A><FONT face=Courier>}, <</FONT><A
href="mailto:#{@v.id}>=#{@v"><FONT
face=Courier>#{@v.id}>=#{@v</FONT></A><FONT face=Courier>}"<BR>
end<BR>end</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier>class Ext < Base<BR> def
initialize()<BR> super<BR> @s =
"Ext::init@#{self.class}"<BR> @v = 2<BR>
end<BR> def dump_ext <BR> puts "<</FONT><A
href="mailto:#{@s.id}>=#{@s"><FONT
face=Courier>#{@s.id}>=#{@s</FONT></A><FONT face=Courier>}, <</FONT><A
href="mailto:#{@v.id}>=#{@v"><FONT
face=Courier>#{@v.id}>=#{@v</FONT></A><FONT face=Courier>}"<BR>
end<BR>end</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier>$y =
Ext.new()<BR>$y.dump_ext()<BR>$y.dump_base()<BR>#------------------------------------------------------------</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Arial>gives me the following result which is kind of
unexpected</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier><84004400>=Base::init@Ext,
<3>=1<BR><84004412>=Ext::init@Ext,
<5>=2<BR><84004412>=Ext::init@Ext, <5>=2<BR></FONT></DIV>
<DIV><FONT face=Arial>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.</FONT></DIV>
<DIV><FONT
face=Courier>
/\ /\<BR>Chochain Lee
--------------------QQQ-^--QQQ-------------------------<BR></FONT></DIV></BODY></HTML>
------ extPart_000_0019_01C0D3FB.F9799240--