How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?
Asked Answered
T

2

29

If I have a class with an attr_accessor, it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating an instance variable, is there a way to get it to create a class variable or a class instance variable instead?

Tigress answered 21/5, 2009 at 23:15 Comment(1)
Work your way through "Seeing Metaclasses Clearly"; it's very helpful when trying to understand the way Ruby's classes and objects work.Periphery
N
46

Like this:

class TYourClass
  class << self
    attr_accessor :class_instance_variable
  end
end

You can look at this as opening the metaclass of the class (of which the class itself is an instance) and adding an attribute to it.

attr_accessor is a method of class Class, it adds two methods to the class, one which reads the instance variable, and other that sets it. Here's a possible implementation:

class Class
  def my_attr_accessor(name)
    define_method name do
      instance_variable_get "@#{name}"
    end 
    define_method "#{name}=" do |new_val|
      instance_variable_set "@#{name}", new_val
    end
  end
end

Completely untested class attribute accessor:

class Class
  def class_attr_accessor(name)
    define_method name do
      class_variable_get "@@#{name}"
    end 
    define_method "#{name}=" do |new_val|
      class_variable_set "@@#{name}", new_val
    end
  end
end
Nador answered 21/5, 2009 at 23:18 Comment(4)
Thanks. I like your way of saying that it opens the metaclass of the class and sorts of adds an addendum. But why does it produce a class instance variable instead of a class variable? And would it be possible to get it to produce a class variable also or would this syntax only work for class instance variables?Tigress
I'ts different, see answer againNador
This will only work for class instance variables, b/c attr_* defines methods for instance variables (of some object).Resolvable
There are some "," missing between arguments to instance_variable_set and class_variable_set but the edit is too small to submit.Abdella
S
21

In Rails, (or anywhere you do require 'active_support') you can use cattr_accessor :name to get the true class variable accessors.

The class instance variables that others have pointed out are usually more useful. The APIdock cattr_accessor page has some helpful discussion clarifying when you would want one not the other, plus the source to the cattr_accessor, cattr_reader and cattr_writer functions.

Sleight answered 3/11, 2009 at 7:26 Comment(1)
much easier - didn't know rails offered this. Thanks!Biancabiancha

© 2022 - 2024 — McMap. All rights reserved.