From: dblack@... Date: 2006-11-16T21:01:14+09:00 Subject: Re: newbie question about attr_reader Hi -- On Thu, 16 Nov 2006, ravi rao wrote: > Hi, > Why cant i have an attribute reader for a class variable. Class variables are shared among many objects (the class, its subclasses, all instances of all those classes), so they're not really the right choice for representing an "attribute", which is generally understood to be a property of a particular object. You can certain write wrapper get/set methods around class variables, and even automate the process. You can also give your classes their own attributes; since classes are objects, they can have attributes like other objects. To do that, you need to call attr_* in the context of the class's singleton class: class C class << self attr_accessor :x end end Now, the class object C has an attribute "x": C.x = 1 puts C.new.class.x # 1 etc. David -- David A. Black | dblack@rubypal.com Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org