0

I don't think there is a distinct difference between class variable and instance variable. The class variable can also be seen as an instance variable. Am I right?

For example:

class Test
  @class_var = 'hello world'

  def self.show_class_var
    @class_var
  end

  def show_class_var
    puts self.class.show_class_var
  end
end

So, I figure we can treat a class variable as the instance variable of the current class (Test). If we define @@class_var = 'hello world', the only benefit is that we can directly refer @@class_var in the instance method. I just want to know if I am right. Am I?

Mischa
  • 42,876
  • 8
  • 99
  • 111
ruanhao
  • 4,663
  • 6
  • 28
  • 43
  • 8
    What you call "class variable" is not a class variable. It's an instance variable on a class object. Real class variables are prepended with `@@` – Sergio Tulentsev Jun 24 '13 at 13:24
  • possible duplicate of [instance variable, class variable and the difference between them in ruby](http://stackoverflow.com/questions/7329954/instance-variable-class-variable-and-the-difference-between-them-in-ruby) – quetzalcoatl Jun 24 '13 at 13:34
  • Thank you guys for answering my question:) Class variable can be seen in subclass while instance variable can not. i think this is the main difference. ( i did not expect a quarrel for my question... i don't know what happen. but i still thank you for your patience ) – ruanhao Jun 25 '13 at 02:03

3 Answers3

3

I think "instance variable, class variable and the difference between them in ruby" has a good explanation of the difference between local, instance and class variables.

Community
  • 1
  • 1
mario
  • 1,248
  • 9
  • 9
  • 1
    Rather than say "this post", which is as bad as "click here", use something more descriptive for the anchor text. It helps those who are reading your answer decide whether they want to follow it, and it helps when the answer is indexed by a search engine. See the W3 recommendation "[Don't use 'click here' as link text](http://www.w3.org/QA/Tips/noClickHere)" – the Tin Man Jun 24 '13 at 15:04
2

i don't think there is a distinct difference between class variable and instance variable.

No there is a subtle difference. As Wayne Conrad states in his answer here:

A class variable (@@) is shared among the class and all of its descendants.

A class instance variable (@) is not shared by the class's descendants.

@@ : class variable( I will recommend the use of class instance variable instead. of saying class variable,which will reduce confusion,that newbie people generally did.)

class Foo;end
#setting class variable @@var
Foo.class_variable_set(:@@var, "good morning!")
#getting class variable @@var via Foo
Foo.class_variable_get(:@@var) # => "good morning!"
class Bar < Foo ;end
#getting class variable @@var via Bar subclass of Foo
Bar.class_variable_get(:@@var) # => "good morning!"

@ : instance variable

class Foo;end
foo = Foo.new
#setting instance variable @var
foo.instance_variable_set(:@var, "good morning!")
#getting instance variable @var via foo object
foo.instance_variable_get(:@var) # => "good morning!"
class Bar < Foo ;end
bar = Bar.new
#getting instance variable @var via bar object,whose class is a subclass of Foo
bar.instance_variable_get(:@var) # => nil 
Community
  • 1
  • 1
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
2

+1 OMG's answer, with a twist: don't forget that classes are objects in their own right, which means there's a third not-exactly class but not-exactly-but-actually instance type of variable as well, for the class itself.

Example:

class A
  # instance variable of objects of class A (and its subclasses)
  # not shared between instances
  def foo; @foo; end
  def foo=(foo); @foo = foo; end

  class << self
    # instance variable of class A
    # not available to subclasses of A
    def bar; @bar; end
    def bar=(bar); @bar = bar; end
  end

  # class variable of A
  # shared by subclasses of A
  def baz; @@baz; end
  def baz=(baz); @@baz = baz; end
end

class B < A; end

Using the above, you get:

a = A.new
b = B.new

a.foo = :foo
a.class.bar = :bar
a.baz = :baz

p a.foo         # :foo
p a.class.bar   # :bar
p a.baz         # :baz

p b.foo         # nil
p b.class.bar   # nil
p b.baz         # :baz
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • Thanks for mentioning the same,I was on the half way for the same,but now deleted those lines from my editor. Thanks for the help :)) – Arup Rakshit Jun 24 '13 at 13:38
  • I didn't down-vote... But its heavily raining now-a-days.. I am totally frustrated for the same.. I did *+1* to you.. no reason for down voting I think :( – Arup Rakshit Jun 24 '13 at 14:08
  • I hear you. To me the most irritating was yesterday: I had an accepted answer that got turned into a comment, for highlighting an outside of the box solution instead of saying how to fix inane code. Oh well... – Denis de Bernardy Jun 24 '13 at 14:12
  • This one :) http://stackoverflow.com/questions/17263065/stacking-ternary-expressions#c – Denis de Bernardy Jun 24 '13 at 14:19