2

As an experiment, I wrote some code that looks like

class MyClass

    @var = 100

    def hello
      p "hello"
    end

end

I know that if I do MyClass.new that @var is not defined on that object, rather I think this would define @var on the class MyClass.

Is there a practical usage to this?

0

2 Answers 2

7

It does have a use: Class variables. The normal Ruby class variable implementation, @@, shares the same variable between a superclass and its subclasses:

class A
  @@v = 0
  def self.v; @@v; end
  def self.v=(val); @@v=val; end
end
class B < A; end
A.v #-> 0
A.v= 3 #-> 3
B.v #->3
B.v= 42 #-> 42
A.v #-> 42

Obviously, this is pretty useless (except for the fact that, unlike class instance variables, class variables are also accessible directly in instance methods, as opposed to through self.class). But the same example with class instance variables:

class A
  @v = 0
  def self.v; @v; end
  def self.v=(val); @v=val; end
end
class B < A; end
A.v #-> 0
A.v= 3 #-> 3
B.v= 42 #-> 42
A.v #-> 3

Also, class instance variables can harness all of the metaprogramming already written for instance variables, like so:

class Foo
  class << self
    attr_accessor :v #Uses a class instance variable
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

your second example will not work, how would you call instance method on class receiver?
@kamal: Whoops. Fixed!
1

If you make accessor methods for @var

MyClass.singleton_class.class_eval { attr_accessor :var }

then you can treat @var like a class variable. The difference is that it won't be inherited by subclasses of MyClass.

_why takes advantage of this in Dwemthy's Array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.