Setting class variable from parent class?

M

Marco

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
 
A

ara.t.howard

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


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
 
D

dblack

Hi --

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 ([email protected])
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
 
M

Mauricio Fernandez

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
 
D

dblack

Hi --

... 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-talk/19972, and
Matz's response.


David

--
David A. Black ([email protected])
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top