Trouble with inherited

S

Steve Tuckner

This is a multi-part message in MIME format.

------=_NextPart_000_0C14_01C3529D.6058A0B0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Anyone have an idea of why the following code doesn't work?

class A
class << self
def inherited(subclass)
name = subclass.my_name
puts "the inheriting classes name is '#{name}'"
end
end
end

class B < A
class << self
def my_name
"the B class"
end
end
end

It fails with:

t.rb:4:in `inherited': undefined method `my_name' for B:Class
(NameError)
from t.rb:10

Steve Tuckner




------=_NextPart_000_0C14_01C3529D.6058A0B0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>



<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D321350816-25072003>Anyone have an idea=20
of why the following code doesn't work?</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D321350816-25072003></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D321350816-25072003>class =

A<BR>&nbsp;&nbsp; class &lt;&lt; self<BR>&nbsp;&nbsp;&nbsp; def=20
inherited(subclass)<BR>&nbsp;&nbsp;&nbsp; &nbsp;name =3D=20
subclass.my_name<BR>&nbsp;&nbsp;&nbsp;&nbsp; puts "the inheriting =
classes name=20
is =
'#{name}'"<BR>&nbsp;&nbsp;end<BR>&nbsp;end<BR>end</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D321350816-25072003>class =
B &lt;=20
A<BR>&nbsp;class &lt;&lt; self<BR>&nbsp;&nbsp; &nbsp;def=20
my_name<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "the B =
class"<BR>&nbsp;=20
&nbsp;&nbsp;end<BR>&nbsp;end<BR>end<BR></SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D321350816-25072003>It =
fails with:=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D321350816-25072003></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D321350816-25072003>t.rb:4:in=20
`inherited': undefined method `my_name' for B:Class=20
(NameError)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from=20
t.rb:10</DIV></SPAN></FONT>
<P><FONT face=3DArial size=3D2>Steve Tuckner</FONT> <BR></P>
<DIV>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0C14_01C3529D.6058A0B0--
 
T

ts

S> Anyone have an idea of why the following code doesn't work?

::inherited is called before the content of the class is evaluated (i.e.
before B::my_name is defined)


Guy Decoux
 

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

Similar Threads


Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top