Newbie Class variable question

Hello!

Forgive my newbie-ness.

Using ruby 1.8, while trying to create a similar example of
one I found to the book called “The Ruby Way”:

class Foo

@@bar = 10

end

Foo.bar = 4

I get:

elathan@velka:~/src/ruby> ruby test.rb
test.rb:7: undefined method `bar=’ for Foo:Class (NoMethodError)

Is this behaviour normal?

Enjoy,

···


University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky

elathan@velka:~/src/ruby> ruby test.rb
test.rb:7: undefined method `bar=' for Foo:Class (NoMethodError)

Is this behaviour normal?

Yes, what you want is a class *instance* variable

svg% cat b.rb
#!/usr/bin/ruby
class A
   class << self
      attr_accessor :a
   end
end

A.a = 12
p A.a
svg%

svg% b.rb
12
svg%

a class variable can be seen as a shared variable

svg% cat b.rb
#!/usr/bin/ruby
class A
   @@a = 12

   def self.a
      p @@a
   end

   def a
      p @@a
   end
end

class B < A
   def self.b
      p @@a
   end

   def b
      p @@a
   end
end

A.a
A.new.a

B.b
B.new.b
svg%

svg% b.rb
12
12
12
12
svg%

Guy Decoux

Actually, I want an easy way to access a class variable from
another class. I construct objects, which I want them to
carry an array specific to their class:

class Foo
@@info = …

end

class Bar
@@info = …

end

As I see from your reply, the only way to access a class
variable from another class (or outside the class in general)
is to define self.foo and foo methods; foo stands for the class
var.

This sounds a little bit wierd to me, since, in the book[*] I have,
the author accesses a class variable implicitly, without having
define self.foo and foo inside the class.

Thanks for your reply!

[*] The Ruby Way - Hal Fulton

Enjoy,

···

On Sun, Oct 12, 2003 at 09:21:24PM +0900, ts wrote:

elathan@velka:~/src/ruby> ruby test.rb
test.rb:7: undefined method `bar=’ for Foo:Class (NoMethodError)

Is this behaviour normal?

Yes, what you want is a class instance variable


University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky

Hi –

This sounds a little bit wierd to me, since, in the book[*] I have,
the author accesses a class variable implicitly, without having
define self.foo and foo inside the class.

Class variables are directly visible only down the class hierarchy:

class A
@@x = 1
end

class B < A
puts @@x # 1
end

You can give access to them, but you have to do it explicity:

class A
def self.peek # technically you don’t have to call it info :slight_smile:
@@info
end
def self.poke(x)
@@info = x
end
end

(This is similar to the kind of get/set methods that are often
implemented using instance variables, but the resemblance is
superficial. Class variables are a different creature, and therfore
not a drop-in replacement for instance variables.)

Just to try to get it all clear: do you have a page number in The Ruby
Way for the example you mentioned?

David

···

On Mon, 13 Oct 2003, Elias Athanasopoulos wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Yes. It’s on page 250.

As I see it now, the author has defined Metal.current_temp=(x).

I apologize for the confusion and I’m thankfull for your
detailed replies. :slight_smile:

Now it’s all clear to me.

Enjoy,

···

On Mon, Oct 13, 2003 at 05:23:46AM +0900, dblack@superlink.net wrote:

Just to try to get it all clear: do you have a page number in The Ruby
Way for the example you mentioned?


University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky