Another Newbie question regarding instance variables?

The value of an instance variable is only available to the instance of the class, whereas the value of the class variable is available to all instances of the class? Is this correct?

If so the power of an instance variable is then to discern between make-ups of similar instances of the same class.

Does this sound correct??

Thanks.
SA

“montana” montana@buc99.bsd.st wrote in message
news:200211131633.gADGXeFJ000838@buc99.bsd.st…

The value of an instance variable is only available to the instance of the
class, whereas the value of the class variable is available to all instances
of the class? Is this correct?

If so the power of an instance variable is then to discern between
make-ups of similar instances of the same class.

Does this sound correct??

Thanks.
SA

The class itself is an object too. It’s an instance of some class. As well
as other objects it can have its own variables. Those are called “class
variables” by consideration. But generally they are instance variables too.
You should take a closer look at the notion of metaclasses.

Hello montana,

Wednesday, November 13, 2002, 7:33:46 PM, you wrote:

The value of an instance variable is only available to the instance of the class, whereas the value of the class variable is available to all instances of the class? Is this correct?

If so the power of an instance variable is then to discern between make-ups of similar instances of the same class.

Does this sound correct??

yes. while all people are mammals, they have different age, sex and
ruby knowledge

···


Best regards,
Bulat mailto:bulatz@integ.ru

The value of an instance variable is only available to the instance of
the class,

An instance variable can be seen as a _private_ variable for an object. To
make it accessible you must create accessor methods.

whereas the value of the class variable is available to all
instances of the class? Is this correct?

A class variable (i.e. @@var) can be seen as a shared variable.

Guy Decoux

Aleksei Guzev wrote:

“montana” montana@buc99.bsd.st wrote in message
news:200211131633.gADGXeFJ000838@buc99.bsd.st…

The value of an instance variable is only available to the instance of the

class, whereas the value of the class variable is available to all instances
of the class? Is this correct?

If so the power of an instance variable is then to discern between

make-ups of similar instances of the same class.

Does this sound correct??

Thanks.
SA

The class itself is an object too. It’s an instance of some class. As well
as other objects it can have its own variables. Those are called “class
variables” by consideration. But generally they are instance variables too.
You should take a closer look at the notion of metaclasses.

Actually, it’s clearer to call them “class instance variables”.

class C
@civ = 0 # class instance variable
@@cv = 1 # class variable
def foo
@iv = 2 # instance variable
p @civ
p @@cv
p @iv
end
end

c = C.new.foo
# ==> nil because c has no @civ defined
# 1 shared by C and c
# 2

I thought that I knew Ruby pretty well until I came accross this thread
about “class instance variables”. It always haunted me why it does not work
when I do

@var = value

outside any method definition, meaning initialization of an instance
variable. Now I see why. Hence the question: is there any practical use for
“class instance variables”? If so, would anybody post an example?

Thanks,
Gennady Bystritsky.

···

----- Original Message -----
From: “Joel VanderWerf” vjoel@PATH.Berkeley.EDU
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, November 13, 2002 11:23 PM
Subject: Re: Another Newbie question regarding instance variables?

Aleksei Guzev wrote:

“montana” montana@buc99.bsd.st wrote in message
news:200211131633.gADGXeFJ000838@buc99.bsd.st…

The value of an instance variable is only available to the instance of
the

class, whereas the value of the class variable is available to all
instances
of the class? Is this correct?

If so the power of an instance variable is then to discern between

make-ups of similar instances of the same class.

Does this sound correct??

Thanks.
SA

The class itself is an object too. It’s an instance of some class. As
well
as other objects it can have its own variables. Those are called “class
variables” by consideration. But generally they are instance variables
too.
You should take a closer look at the notion of metaclasses.

Actually, it’s clearer to call them “class instance variables”.

class C
@civ = 0 # class instance variable
@@cv = 1 # class variable
def foo
@iv = 2 # instance variable
p @civ
p @@cv
p @iv
end
end

c = C.new.foo
# ==> nil because c has no @civ defined
# 1 shared by C and c
# 2

Hi –

I thought that I knew Ruby pretty well until I came accross this thread
about “class instance variables”. It always haunted me why it does not work
when I do

@var = value

outside any method definition, meaning initialization of an instance
variable. Now I see why. Hence the question: is there any practical use for
“class instance variables”? If so, would anybody post an example?

One potentially good thing, though I can’t claim to have any dazzling
examples, is that a class’s instance variables can be used as true
class-scoped variables, as opposed to class variables which are sort
of tree-scoped.

For example:

class A
class << self; attr_accessor :var; end
@var = 0
def increment
self.class.var += 1
puts “New value: #{self.class.var}”
end
end

a = A.new
a.increment # New value: 1
b = A.new
b.increment # New value: 2

class B < A
@var = 100
end

c = B.new
c.increment # New value: 101
b.increment # New value: 3

David

···

On Fri, 15 Nov 2002, Gennady F. Bystritsky wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Cool. Never thought of it this way. And what’s the buzz in another thread
about making them private? Does it mean that as it currently implemented you
can access such variables even directly without accessor methods?

Gennady.

···

----- Original Message -----
From: dblack@candle.superlink.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, November 14, 2002 8:32 AM
Subject: Re: Another Newbie question regarding instance variables?

Hi –

On Fri, 15 Nov 2002, Gennady F. Bystritsky wrote:

I thought that I knew Ruby pretty well until I came accross this thread
about “class instance variables”. It always haunted me why it does not
work
when I do

@var = value

outside any method definition, meaning initialization of an instance
variable. Now I see why. Hence the question: is there any practical use
for
“class instance variables”? If so, would anybody post an example?

One potentially good thing, though I can’t claim to have any dazzling
examples, is that a class’s instance variables can be used as true
class-scoped variables, as opposed to class variables which are sort
of tree-scoped.

For example:

class A
class << self; attr_accessor :var; end
@var = 0
def increment
self.class.var += 1
puts “New value: #{self.class.var}”
end
end

a = A.new
a.increment # New value: 1
b = A.new
b.increment # New value: 2

class B < A
@var = 100
end

c = B.new
c.increment # New value: 101
b.increment # New value: 3

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

dblack@candle.superlink.net wrote:

Hi –

I thought that I knew Ruby pretty well until I came accross this thread
about “class instance variables”. It always haunted me why it does not work
when I do

@var = value

outside any method definition, meaning initialization of an instance
variable. Now I see why. Hence the question: is there any practical use for
“class instance variables”? If so, would anybody post an example?

One potentially good thing, though I can’t claim to have any dazzling
examples, is that a class’s instance variables can be used as true
class-scoped variables, as opposed to class variables which are sort
of tree-scoped.

A slightly more elaborate example, showing how it can be useful to have
per-class rather than per-class-tree variables:

class A
class << self
def new(*args)
@count ||= 0
@count += 1
super
end

 attr_reader :count

end
end

class B < A
end

10.times { A.new }
20.times { B.new }

p A.count # ==> 10
p B.count # ==> 20

Note that count is the number of direct instances of the class (A or
B) which have been created, not the number that currently exist.

···

On Fri, 15 Nov 2002, Gennady F. Bystritsky wrote:

Hi –

Cool. Never thought of it this way. And what’s the buzz in another thread
about making them private? Does it mean that as it currently implemented you
can access such variables even directly without accessor methods?

I think that’s a different thing – having instance variables (i.e.,
of non-class objects) be scoped to the class. That would result
in:

class A
def learn
@_x = 1
end
end

class B < A
def speak
puts @_x
end
end

b = B.new
b.learn
b.speak # Currently prints 1
# If @_x were “class local”, the @_x in A and
# the @_x in B would be different and this
# would print nil.

At least that’s how I understand that discussion. So it’s different
from (though might intersect with, in some cases) the thing about
a Class’s own instance variables and accessors.

David

···

On Fri, 15 Nov 2002, Gennady F. Bystritsky wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

I thought that I knew Ruby pretty well until I came accross this thread
about “class instance variables”. It always haunted me why it does not
work
when I do

@var = value

outside any method definition, meaning initialization of an instance
variable. Now I see why. Hence the question: is there any practical use
for
“class instance variables”? If so, would anybody post an example?

One potentially good thing, though I can’t claim to have any dazzling
examples, is that a class’s instance variables can be used as true
class-scoped variables, as opposed to class variables which are sort
of tree-scoped.

A slightly more elaborate example, showing how it can be useful to have
per-class rather than per-class-tree variables:

class A
class << self
def new(*args)
@count ||= 0
@count += 1
super
end

 attr_reader :count

end
end

class B < A
end

10.times { A.new }
20.times { B.new }

p A.count # ==> 10
p B.count # ==> 20

Note that count is the number of direct instances of the class (A or
B) which have been created, not the number that currently exist.

This one is good, now I have a clear picture of the concept.

···

----- Original Message -----
From: “Joel VanderWerf” vjoel@PATH.Berkeley.EDU
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, November 14, 2002 9:10 AM
Subject: Re: Another Newbie question regarding instance variables?

dblack@candle.superlink.net wrote:

On Fri, 15 Nov 2002, Gennady F. Bystritsky wrote: