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

On 11/16/06, ravi rao [email protected] wrote:

Hi,
Why cant i have an attribute reader for a class variable.
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.

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

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” [email protected] wrote:

end
attr_reader :var1
puts(s1.var1)

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

On Nov 16, 2006, at 1:01 PM, [email protected] 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

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

[email protected]:~/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
[email protected]:~/tmp$ ruby foo.rb
0
1

Hi –

On Fri, 17 Nov 2006, Xavier N. wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs