What does this warning actually mean?

  • Thread starter Michael T. Richter
  • Start date
M

Michael T. Richter

--=-Tn+ojkiOZAo6sC2h3S19
Content-Type: multipart/alternative; boundary="=-EYlAYUk/EEJikpzkd0gn"


--=-EYlAYUk/EEJikpzkd0gn
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

So, I'm trying my hand at metaprogramming and am finding this the result
of executing my code with -w enabled:

warning: class variable access from toplevel singleton method

(This is spammed ad infinitum anywhere I use the overridden gets, but
it's always the same message and always the same cause: gets.)

The code it's pointing to looks like this:

def override_methods(io_in)
puts io_in
class << io_in
alias :eek:ld_gets :gets
@@override_buffer=3D[]
def gets separator =3D $/
raise ArgumentError, "cannot use non-standard
separator" unless separator =3D=3D $/

unless line =3D @@override_buffer.shift
return old_gets
else
return line
end
end
def find_end
for a in 1..20
line =3D self.old_gets
if line =3D=3D "__END__\n"
@@override_buffer =3D []
break
else
@@override_buffer << line
end
end
self
end
end
io_in
end

def find_block_begin io_in
io_in =3D override_methods(io_in)
io_in.find_end
end

To me the culprit is likely that @@override_buffer thing, but I can't
for the life of me figure out how to get around it. My modified gets
needs access to that (shared) buffer that the added find_end method
generates while peeking ahead. @override_buffer and override_buffer
don't work (the latter for reasons obvious to me, the former ... not so
obvious).

Now, the output of the code is exactly what I expect it to be, so this
isn't a show-stopper. I am, however, in the habit of making my code -w
clean if it's at all possible. How would I do it with that snippet?

--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
In his errors a man is true to type. Observe the errors and you will
know the man. (=E5=AD=94=E5=A4=AB=E5=AD=90)

--=-EYlAYUk/EEJikpzkd0gn
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; CHARSET=3DUTF-8">
<META NAME=3D"GENERATOR" CONTENT=3D"GtkHTML/3.12.1">
</HEAD>
<BODY>
So, I'm trying my hand at metaprogramming and am finding this the result of=
executing my code with -w enabled:<BR>
<BR>
<TT><B>warning: class variable access from toplevel singleton method</B></T=
T><BR>
<BR>
(This is spammed ad infinitum anywhere I use the overridden gets, but it's =
always the same message and always the same cause: gets.)<BR>
<BR>
The code it's pointing to looks like this:<BR>
<BR>
<TT>def override_methods(io_in)</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; alias :eek:ld_gets :gets</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; @@override_buffer=3D[]</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; def gets separator =3D $/</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise A=
rgumentError, &quot;cannot use non-standard separator&quot; unless separato=
r =3D=3D $/</TT><BR>
<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unless =
line =3D @@override_buffer.shift</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp; @@override_buffer &lt;&lt; line</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self</T=
T><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; end</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; io_in</TT><BR>
<TT>end</TT><BR>
<BR>
<TT>def find_block_begin io_in</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; io_in =3D override_methods(i=
o_in)</TT><BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; io_in.find_end</TT><BR>
<TT>end</TT><BR>
<BR>
To me the culprit is likely that @@override_buffer thing, but I can't for t=
he life of me figure out how to get around it.&nbsp; My modified gets needs=
access to that (shared) buffer that the added find_end method generates wh=
ile peeking ahead.&nbsp; @override_buffer and override_buffer don't work (t=
he latter for reasons obvious to me, the former ... not so obvious).<BR>
<BR>
Now, the output of the code is exactly what I expect it to be, so this isn'=
t a show-stopper.&nbsp; I am, however, in the habit of making my code -w cl=
ean if it's at all possible.&nbsp; How would I do it with that snippet?<BR>
<BR>
<TABLE CELLSPACING=3D"0" CELLPADDING=3D"0" WIDTH=3D"100%">
<TR>
<TD>
-- <BR>
<B>Michael T. Richter</B> &lt;<A HREF=3D"mailto:[email protected]">ttmri=
(e-mail address removed)</A>&gt; (<B>GoogleTalk:</B> (e-mail address removed))<BR>
<I>In his errors a man is true to type. Observe the errors and you will kno=
w the man. (孔夫子)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-EYlAYUk/EEJikpzkd0gn--

--=-Tn+ojkiOZAo6sC2h3S19
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQBGsdIZLqyWkKVQ54QRAs+lAJ0eWYi5gR8BOxLURrqmolgGEeIfFwCg2TrB
X0GcRxEMzw5PzWol68NhL9k=
=Y0D4
-----END PGP SIGNATURE-----

--=-Tn+ojkiOZAo6sC2h3S19--
 
M

Michael T. Richter

--=-ZWoVUl+CANwj5sEKN/hd
Content-Type: multipart/related; type="multipart/alternative";
boundary="=-magQAUUPeda0ol1Xru7M"


--=-magQAUUPeda0ol1Xru7M
Content-Type: multipart/alternative; boundary="=-Ay3bRzRIfYDpv0KwQTiW"


--=-Ay3bRzRIfYDpv0KwQTiW
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Michael I got no clue what you are trying to do here, but maybe the
following is hint enouh, BTW, just do not use class variables ;).
522/23 > cat test1.rb && ruby test1.rb
=20
class << self
@@a=3D42
def a
@@a +=3D1
end
end
puts a
test1.rb:3: warning: class variable access from toplevel singleton method
test1.rb:5: warning: class variable access from toplevel singleton method
test1.rb:5: warning: class variable access from toplevel singleton method
43
=20
---------------------------------------------
=20
523/24 > cat test2.rb && ruby test2.rb
=20
class << self
def a
@a||=3D42
@a+=3D1
end
end
puts a
=20
=20
43


This much I figured out for myself, Robert. ;) The problem is that I
need to share a variable across two functions -- a buffer, to be
precise. One function fills the buffer while the other consumes from
it. The functions are to be singleton methods on an instantiated IO
object. I can't see how to make them share an instance variable without
doing funky things like calling instance_variable_set on the object to
provide it with said instance variable before adding the singletons.
Using a class variable was a desperate test after running headlong into
a brick wall trying to share them.

As it is now, I've got it working with an additional call to
instance_variable_set before defining the singletons, but it looks kind
of clunky and I was hoping there was a more elegant approach available.


--=20
Michael T. Richter <[email protected]> (GoogleTalk:
(e-mail address removed))
Our outrage at China notwithstanding, we should remember that before
1891 the copyrights of foreigners were not protected in the United
States. (Lawrence Lessig)

--=-Ay3bRzRIfYDpv0KwQTiW
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; CHARSET=3DUTF-8">
<META NAME=3D"GENERATOR" CONTENT=3D"GtkHTML/3.12.1">
</HEAD>
<BODY>
On Thu, 2007-02-08 at 22:04 +0900, Robert Dober wrote:
<BLOCKQUOTE TYPE=3DCITE>
<PRE>
<FONT COLOR=3D"#000000">Michael I got no clue what you are trying to do her=
e, but maybe the</FONT>
<FONT COLOR=3D"#000000">following is hint enouh, BTW, just do not use class=
variables ;).</FONT>
<FONT COLOR=3D"#000000">522/23 &gt; cat test1.rb &amp;&amp; ruby test1.rb</=
FONT>

<FONT COLOR=3D"#000000"> class &lt;&lt; self</FONT>
<FONT COLOR=3D"#000000"> @@a=3D42</FONT>
<FONT COLOR=3D"#000000"> def a</FONT>
<FONT COLOR=3D"#000000"> @@a +=3D1</FONT>
<FONT COLOR=3D"#000000"> end</FONT>
<FONT COLOR=3D"#000000"> end</FONT>
<FONT COLOR=3D"#000000"> puts a</FONT>
<FONT COLOR=3D"#000000">test1.rb:3: warning: class variable access from top=
level singleton method</FONT>
<FONT COLOR=3D"#000000">test1.rb:5: warning: class variable access from top=
level singleton method</FONT>
<FONT COLOR=3D"#000000">test1.rb:5: warning: class variable access from top=
level singleton method</FONT>
<FONT COLOR=3D"#000000">43</FONT>


<FONT COLOR=3D"#000000">523/24 &gt; cat test2.rb &amp;&amp; ruby test2.rb</=
FONT>

<FONT COLOR=3D"#000000"> class &lt;&lt; self</FONT>
<FONT COLOR=3D"#000000"> def a</FONT>
<FONT COLOR=3D"#000000"> @a||=3D42</FONT>
<FONT COLOR=3D"#000000"> @a+=3D1</FONT>
<FONT COLOR=3D"#000000"> end</FONT>
<FONT COLOR=3D"#000000"> end</FONT>
<FONT COLOR=3D"#000000"> puts a</FONT>


<FONT COLOR=3D"#000000">43</FONT>
</PRE>
</BLOCKQUOTE>
<BR>
This much I figured out for myself, Robert.&nbsp; <IMG SRC=3D"cid:118606421=
(e-mail address removed)" ALIGN=3D"middle" ALT=3D";)" BORDER=
=3D"0">&nbsp; The problem is that I need to share a variable across <B>two<=
/B> functions -- a buffer, to be precise.&nbsp; One function fills the buff=
er while the other consumes from it.&nbsp; The functions are to be singleto=
n methods on an instantiated IO object.&nbsp; I can't see how to make them =
share an instance variable without doing funky things like calling instance=
_variable_set on the object to provide it with said instance variable befor=
e adding the singletons.&nbsp; Using a class variable was a desperate test =
after running headlong into a brick wall trying to share them.<BR>
<BR>
As it is now, I've got it working with an additional call to instance_varia=
ble_set before defining the singletons, but it looks kind of clunky and I w=
as hoping there was a more elegant approach available.<BR>
<BR>
<BR>
<TABLE CELLSPACING=3D"0" CELLPADDING=3D"0" WIDTH=3D"100%">
<TR>
<TD>
-- <BR>
<B>Michael T. Richter</B> &lt;<A HREF=3D"mailto:[email protected]">ttmri=
(e-mail address removed)</A>&gt; (<B>GoogleTalk:</B> (e-mail address removed))<BR>
<I>Our outrage at China notwithstanding, we should remember that before 189=
1 the copyrights of foreigners were not protected in the United States. (La=
wrence Lessig)</I>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

--=-Ay3bRzRIfYDpv0KwQTiW--

--=-magQAUUPeda0ol1Xru7M
Content-ID: <[email protected]>
Content-Disposition: attachment; filename=smiley-4.png
Content-Type: image/png; name=smiley-4.png
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC/UlEQVR42n2Tf2iUdRzHX9/v8zy3
u91ulnO5pXGXUUsK1yIY9ke0CDQiodbQKCOnNieaIkKI/yRhf0QgCeEqViKUYgUOnTE9G5WtjlqB
TV1bsnl0tV93u93ux3PP93meb/8sKBNf8P7jw/v9+fz1/sD/iQI7gB7AWdApoGPB+w/ihvnI/bHK
bfu2NLF2zUrMcAw8G2d+mIGBi+w/nGE46XQBnTc7cOFAZ+MTO1/bgDBCuIVxnPwIQhgYwXqMQA2q
MMahd+O8fWLmAvAkgLGw/MGbOx55fueuF/FKKVRxHF/NIaQFgO+kcQtjgGZ1cwyjOLXiuyG7DuiV
QLSx4fat2zvX4dmTeOVphDCwvUqkWcVk2mfDnkGEtPCdLEJabH95CfctN7YBUQk83XNoMUJaTE+M
cuT476x9pZfmZ3sI1D7D8fMeP/2a4a3uGaqWt6Ly1zBDdbzadhvAowLomrn0eocqXMeoqCVU+xga
E2mYlKb6sSINtG58gzOnTzGf/BR7+hukWUni52u07p04aS5bdudmAN/NURZ3oSe+RBhhhLTQGsqZ
BKd7Pic3fhSVGwLAtaeoX2IAPGem02kQJrP5EHJRHYb0CHhZtG+CdtFeiezIO2h3Hu17OI6Dqxzy
RR8AadvlXu0WqDSm6O87xkzWx6p+AF/l8N08vmejvRLaV7jKJjNnEwwvJTXtASQk0FecOEsoUs/6
9RuJx8+RGOgjuLgZK7ISzBo8X9L/w1+07RnmnqZ2RpNlrg7lAU4IILrq3sh437GnMCruIFD9IL8M
fsvQpUE+OiO4fGWYRVUGhw+2sablIZJXPiN+cYwPT2b4LaUb/mli94Etsfatm5pwi0kisU1Y4RUI
IfHsSTQalR/lcuJj/ph0Od8/y9G46gI6/13lrzrW1bTs3RwmEKxGyAp8lWN2rkShpCkrzfU/FYkf
53j/rPM18PjNnqn77jrZ/kJLFasaQ4SDglJZk533GUspvjiXZSTFJ8BL3IIosBv4HlBAGrgKvAc8
fGP4b4kpTGRKdd96AAAAAElFTkSuQmCC


--=-magQAUUPeda0ol1Xru7M--

--=-ZWoVUl+CANwj5sEKN/hd
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQBGseffLqyWkKVQ54QRAtS5AJ4yY88Q20LTKkFZcmQn6j8bkT6elQCcD9PJ
sWp87D8tT3azMWScm8aIcTo=
=iWaE
-----END PGP SIGNATURE-----

--=-ZWoVUl+CANwj5sEKN/hd--
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top