From: "Michael T. Richter" Date: 2007-08-02T23:19:35+09:00 Subject: Re: What does this warning actually mean? --=-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 On Thu, 2007-02-08 at 22:04 +0900, Robert Dober wrote: > 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 (GoogleTalk: ttmrichter@gmail.com) 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 On Thu, 2007-02-08 at 22:04 +0900, Robert Dober wrote:
Michael I got no clue what you are trying to do her=
e, but maybe the
following is hint enouh, BTW, just do not use class=
 variables ;).
522/23 > cat test1.rb && ruby test1.rb

  class << self
    @@a=3D42
    def a
      @@a +=3D1
    end
  end
  puts a
test1.rb:3: warning: class variable access from top=
level singleton method
test1.rb:5: warning: class variable access from top=
level singleton method
test1.rb:5: warning: class variable access from top=
level singleton method
43

---------------------------------------------

523/24 > cat test2.rb && ruby test2.rb

  class << self
    def a
      @a||=3D42
      @a+=3D1
    end
  end
  puts a


43

This much I figured out for myself, Robert.  3D";)"  The problem is that I need to share a variable across two<= /B> functions -- a buffer, to be precise.  One function fills the buff= er while the other consumes from it.  The functions are to be singleto= n 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 befor= e 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_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.


--
Michael T. Richter <ttmri= chter@gmail.com> (GoogleTalk: ttmrichter@gmail.com)
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)
--=-Ay3bRzRIfYDpv0KwQTiW-- --=-magQAUUPeda0ol1Xru7M Content-ID: <1186064213.28943.18.camel@localhost.localdomain> 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--