Accessing class instance variable from instance method

But you're trying to access a (private) variable on a completely
different, and only vaguely related, object. Why should you have a
"nice" way?

Then perhaps Ruby should not provide a "nice" way to access @@var too

:slight_smile:

Ruby's all about niceness :slight_smile:
(Sorry for the preaching tone earlier). I really liked Ara's solution
with (semi-constant) class variables. Why is @@var failing you, and you
have to use class@var instead?

Mehr, Assaph (Assaph) wrote:

Then perhaps Ruby should not provide a "nice" way to access @@var too

:slight_smile:

Ruby's all about niceness :slight_smile:
(Sorry for the preaching tone earlier). I really liked Ara's solution
with (semi-constant) class variables. Why is @@var failing you, and you
have to use class@var instead?

Simply because @@var is shared between subclasses.

  $ irb
  irb(main):001:0> class Parent; @@var=1; def p_var; p @@var end end
  => nil
  irb(main):002:0> class Child < Parent; @@var = 2 end
  => nil
  irb(main):003:0> Parent.new.p_var
  2
  => nil

I know there might be some cases where you want to share your class vars with the subclasses, but I usually want a per-class class variables.

I believe this will change in Ruby2.

路路路

--
dave

Hi,

At Wed, 9 Jun 2004 13:10:52 +0900,
David Garamond wrote in [ruby-talk:102895]:

I know there might be some cases where you want to share your class vars
with the subclasses, but I usually want a per-class class variables.

I believe this will change in Ruby2.

It was changed in 1.9.

路路路

--
Nobu Nakada

Thanks, Nobu. I'd love to use it now though :slight_smile:

What will be the difference between class instance var (@var) and @@var?

路路路

nobu.nokada@softhome.net wrote:

At Wed, 9 Jun 2004 13:10:52 +0900,
David Garamond wrote in [ruby-talk:102895]:

I know there might be some cases where you want to share your class vars with the subclasses, but I usually want a per-class class variables.

I believe this will change in Ruby2.

It was changed in 1.9.

--
dave

so - does a concept of 'inherited class variable' exist in 1.9 then? i
actually use these from time to time...

-a

路路路

On Wed, 9 Jun 2004 nobu.nokada@softhome.net wrote:

Hi,

At Wed, 9 Jun 2004 13:10:52 +0900,
David Garamond wrote in [ruby-talk:102895]:

I know there might be some cases where you want to share your class vars
with the subclasses, but I usually want a per-class class variables.

I believe this will change in Ruby2.

It was changed in 1.9.

--
Nobu Nakada

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it; and a weed grows, even though we do
not love it. --Dogen

===============================================================================

Hi --

> At Wed, 9 Jun 2004 13:10:52 +0900,
> David Garamond wrote in [ruby-talk:102895]:
>
>>I know there might be some cases where you want to share your class vars
>>with the subclasses, but I usually want a per-class class variables.
>>
>>I believe this will change in Ruby2.
>
> It was changed in 1.9.

Thanks, Nobu. I'd love to use it now though :slight_smile:

What will be the difference between class instance var (@var) and @@var?

I think it will be the same fundamental difference as now: instance
vars are visible only when their owner is 'self', whereas class
variables are visible inside instance method definitions (when 'self'
is not the class or module object). Just minus the per-hierarchy
thing. <editorial>I wish Matz had chosen something other than '@@'
for class variables, since it often generates the impression that
they're somehow akin to instance variables.</editorial>

David

路路路

On Wed, 9 Jun 2004, David Garamond wrote:

nobu.nokada@softhome.net wrote:

--
David A. Black
dblack@wobblini.net

Ara.T.Howard wrote:

Hi,

At Wed, 9 Jun 2004 13:10:52 +0900,
David Garamond wrote in [ruby-talk:102895]:

I know there might be some cases where you want to share your class vars
with the subclasses, but I usually want a per-class class variables.

I believe this will change in Ruby2.

It was changed in 1.9.

--
Nobu Nakada

so - does a concept of 'inherited class variable' exist in 1.9 then? i
actually use these from time to time...

I did too, until I saw Nobu's note today. I'm going thru all my code and replacing inherited class vars with accessors to class vars (protected, if need be):

class C
   @history =
   class << self
     attr_reader :history
   end
end

class D < C
   def foo
     C.history << "D#foo"
   end
end

d = D.new
d.foo
p C.history

I like how "C.history" is explicit about which class's var is being accessed, whereas @@ was always ambiguous, depending on initialization order.

路路路

On Wed, 9 Jun 2004 nobu.nokada@softhome.net wrote: