Newbie question about attr_reader

Hi,
Why cant i have an attribute reader for a class variable.
Here's a program.

class SomeClass

@@var1 = 9 #class var
def someMethod
    @@var1 = 1
end

def initialize
    @var2 = 5
end

def getVar1
  return @@var1
end

attr_reader :var2
attr_reader :var1

end #SomeClass ends

  s1 = SomeClass.new
  puts(s1.var2)
  puts(s1.getVar1)
  s1.someMethod
  puts(s1.var2)
  puts(s1.getVar1)
  puts(s1.var1)

Here's the output:

5
9
5
1
nil

Thanks,
Ravi Rao

The reason being that in Ruby, instance variables are exposed to the outside
world via one of the attr family of methods.

···

On 11/16/06, ravi rao <rao.ravi.m@gmail.com> wrote:

Hi,
Why cant i have an attribute reader for a class variable.
Thanks,
Ravi Rao

--
Satish Talim
Learning Ruby - http://rubylearning.com/

Hi --

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

···

On Thu, 16 Nov 2006, ravi rao wrote:

--
                   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] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Hi

The reason is because you are exposing @var1 not @@var1 and with
someMethod you are initializing @@var1 not @var1, so it's nil.

Kind regards

···

On Thu, 16 Nov 2006 18:26:40 +0900 "ravi rao" <rao.ravi.m@gmail.com> wrote:

Hi,
Why cant i have an attribute reader for a class variable.
Here's a program.

class SomeClass

@@var1 = 9 #class var
def someMethod
    @@var1 = 1
end

def initialize
    @var2 = 5
end

def getVar1
  return @@var1
end

attr_reader :var2
attr_reader :var1

end #SomeClass ends

  s1 = SomeClass.new
  puts(s1.var2)
  puts(s1.getVar1)
  s1.someMethod
  puts(s1.var2)
  puts(s1.getVar1)
  puts(s1.var1)

Here's the output:

5
9
5
1
nil

Thanks,
Ravi Rao

______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com

David, technically is there any relationship between the attribute @x here

   class A
     @x = 1
   end

and the @x that accessor deals with? That would be an attribute of what? Is there a singleton instance of the singleton class? Or is that @x an attribute of A as instance of Class? I tried to get this right with examples but I guess the exact answer comes from the actual implementation.

-- fxn

fxn@feynman:~/tmp$ cat foo.rb
class A
   @x = 0
   @y = 1
   class << self
     def x
       @x
     end
     attr_accessor :y
   end
end

puts A.x, A.y
fxn@feynman:~/tmp$ ruby foo.rb
0
1

···

On Nov 16, 2006, at 1:01 PM, dblack@wobblini.net wrote:

  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

Hi --

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

David, technically is there any relationship between the attribute @x here

class A
  @x = 1
end

and the @x that accessor deals with?

Yes; they're the same @x.

That would be an attribute of what? Is there a singleton instance of
the singleton class? Or is that @x an attribute of A as instance of
Class? I tried to get this right with examples but I guess the exact
answer comes from the actual implementation.

@x is an instance variable of A. The accessor methods are defined for
A only, not for all classes. In general, when you do this:

   class << obj
     ...
   end

you're in the singleton class of obj, and whatever instance methods
you define are singleton methods of obj. attr_accessor is just a
mechanism for writing instance methods. So what I did was equivalent
to:

   class A
     class << self
       def x
         @x
       end
       def x=(y)
         @x = y
       end
     end
   end

i.e., adding instance methods to A's singleton class.

David

···

On Fri, 17 Nov 2006, Xavier Noria wrote:

On Nov 16, 2006, at 1:01 PM, dblack@wobblini.net wrote:

--
                   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] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org