[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Setting class variable from parent class?

Marco

4/3/2006 3:45:00 PM

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

--
Posted via http://www.ruby-....


4 Answers

Ara.T.Howard

4/3/2006 3:59:00 PM

0

dblack

4/3/2006 4:08:00 PM

0

Mauricio Fernández

4/3/2006 9:32:00 PM

0

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

--
Mauricio Fernandez - http://eige... - singular Ruby


dblack

4/3/2006 11:44:00 PM

0