Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie Class variable question

Reply
Thread Tools

Newbie Class variable question

 
 
Elias Athanasopoulos
Guest
Posts: n/a
 
      10-12-2003
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



 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      10-12-2003
>>>>> "E" == Elias Athanasopoulos <> writes:

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

E> 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

 
Reply With Quote
 
 
 
 
Elias Athanasopoulos
Guest
Posts: n/a
 
      10-12-2003
On Sun, Oct 12, 2003 at 09:21:24PM +0900, ts wrote:
> E> elathan@velka:~/src/ruby> ruby test.rb
> E> test.rb:7: undefined method `bar=' for Foo:Class (NoMethodError)
>
> E> Is this behaviour normal?
>
> Yes, what you want is a class *instance* variable


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,
--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky



 
Reply With Quote
 
Elias Athanasopoulos
Guest
Posts: n/a
 
      10-12-2003
On Mon, Oct 13, 2003 at 05:23:46AM +0900, wrote:
> Just to try to get it all clear: do you have a page number in The Ruby
> Way for the example you mentioned?


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.

Now it's all clear to me.

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Simple Newbie question about accessing a Variable out of a class of a class Christian Maier C++ 3 02-15-2007 08:24 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Newbie Question: Global variable vs. Top-level variable Sam Kong Ruby 2 05-25-2005 09:47 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57