From: Robert Dober Date: 2007-08-03T00:15:33+09:00 Subject: Re: What does this warning actually mean? ------=_Part_90651_8223882.1186067727074 Content-Type: multipart/alternative; boundary="----=_Part_90650_17794899.1186067727074" ------=_Part_90650_17794899.1186067727074 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 8/2/07, Michael T. Richter wrote: > > 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 thefollowing is hint enouh, BTW, just do not use class variables ;).522/23 > cat test1.rb && ruby test1.rb > class << self @@a=42 def a @@a +=1 end end puts atest1.rb:3: warning: class variable access from toplevel singleton methodtest1.rb:5: warning: class variable access from toplevel singleton methodtest1.rb:5: warning: class variable access from toplevel singleton method43 > --------------------------------------------- > 523/24 > cat test2.rb && ruby test2.rb > class << self def a @a||=42 @a+=1 end end puts a > > 43 > > Michael please post in raw text format, this reads terribly and can probably not go to the newsgroup. This much I figured out for myself, Robert. [image: ;)] > > If you did you should know exactly how to solve the problem, use class instance variables instead of class variables. A sound advice in all cases :) 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. > You see we are not talking instance variables here, actually we are, the important thing is instance variable of what, of the class object, or in your case singleton class object, that will perfectly do the trick. 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. > sure does, sure is. Robert -- > *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)* > > -- [...] as simple as possible, but no simpler. -- Attributed to Albert Einstein ------=_Part_90650_17794899.1186067727074 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline

On 8/2/07, Michael T. Richter <ttmrichter@gmail.com> wrote:
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

  class << self
    @@a=42
    def a
      @@a +=1
    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

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

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

  class << self
    def a
      @a||=42
      @a+=1
    end
  end
  puts a


43

Michael please post in raw text format, this reads terribly and can probably not go to the newsgroup.

This much I figured out for myself, Robert.  ;)
 
If you did you should know exactly how to solve the problem, use class instance variables instead of class variables. A sound advice in all cases :)

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.

You see we are not talking instance variables here, actually we are, the important thing is instance variable of what, of the class object, or in your case singleton class object, that will perfectly do the trick.

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.

sure does, sure is.

Robert

--
Michael T. Richter <ttmrichter@gmail.com> (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)




--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein ------=_Part_90650_17794899.1186067727074-- ------=_Part_90651_8223882.1186067727074 Content-Type: image/png; name=smiley-4.png Content-Transfer-Encoding: base64 Content-ID: <1186064213.28943.18.camel@localhost.localdomain> X-Attachment-Id: 0.0.1 Content-Disposition: inline; filename="smiley-4.png" iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAA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