Forum: Ruby Setting class variable from parent class?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
77fec2b94c481ad3d2b2135b1f07ef5f?d=identicon&s=25 Marco (Guest)
on 2006-04-03 17:44
Hi All!

I'm trying to teach my Parent class how to set variables into its Child
class; ie

class Parent
  def self.set_my_class_variable(value)
    reference_to_my_class.my_variable = value
  end
end

class Child
  set_my_class_variable "hello world"
end

I think what I'm missing is the way to reference the Child class. I've
been wrongly supposing that

  Parent.set_my_class_variable(value)
    @@my_variable = value
  end

would be doing the work. But the '@@' syntax sets the class variable to
Parent.

Thank you,
Marco
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-03 18:02
(Received via mailing list)
On Tue, 4 Apr 2006, Marco wrote:

>
>
> would be doing the work. But the '@@' syntax sets the class variable to
> Parent.
>
> Thank you,
> Marco


     harp:~ > cat a.rb
     class Parent
       class << self
         attr_accessor 'a'
         def inherited child
           child.a = 42
         end
       end
     end

     class Child < Parent
     end

     p Child.a


     harp:~ > ruby a.rb
     42


hth.

-a
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-03 18:11
(Received via mailing list)
Hi --

On Tue, 4 Apr 2006, Marco wrote:

> Hi All!
>
> I'm trying to teach my Parent class how to set variables into its Child
> class; ie

The parent and its children share class variables:

irb(main):001:0> class A; @@var = 2; end
=> 2
irb(main):002:0> class B < A; @@var; end
=> 2

However, that's supposed to change in Ruby 2.0, so that class
variables will more strictly class-scoped (and not hierarchy-scoped).


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2006-04-03 23:34
(Received via mailing list)
On Tue, Apr 04, 2006 at 01:08:24AM +0900, dblack@wobblini.net wrote:
> On Tue, 4 Apr 2006, Marco wrote:
> >I'm trying to teach my Parent class how to set variables into its Child
> >class; ie
>
> The parent and its children share class variables:
>
> irb(main):001:0> class A; @@var = 2; end
> => 2
> irb(main):002:0> class B < A; @@var; end
> => 2

... depending on the assignment order

class A; end
class B < A; @@cv = 1 end
class A; @@cv = 2 end
class A; @@cv end     # => 2
class B; @@cv end     # => 1 # !> class variable @@cv of A is overridden
by B
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-04 01:45
(Received via mailing list)
Hi --

On Tue, 4 Apr 2006, Mauricio Fernandez wrote:

>> => 2
>
> ... depending on the assignment order
>
> class A; end
> class B < A; @@cv = 1 end
> class A; @@cv = 2 end
> class A; @@cv end     # => 2
> class B; @@cv end     # => 1 # !> class variable @@cv of A is overridden by B

I know -- see
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby..., and
Matz's response.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2006-04-05 12:37
(Received via mailing list)
On Tue, Apr 04, 2006 at 08:44:29AM +0900, dblack@wobblini.net wrote:
>>>irb(main):002:0> class B < A; @@var; end
>>>=> 2
>>
>>... depending on the assignment order
[...]
>http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby..., and
>Matz's response.

matz> I know this happens.  I consider this as an error that Ruby does
not
matz> detect yet.

Fortunately some things have changed for the better since then:

>>class A; end
>>class B < A; @@cv = 1 end
>>class A; @@cv = 2 end
>>class A; @@cv end     # => 2
>>class B; @@cv end     # => 1 # !> class variable @@cv of A is overridden by B
                               ================================================
This topic is locked and can not be replied to.