Scope of class variables in Ruby: Between parent and child classes

Han Lee
2 min readAug 3, 2016

--

In Ruby, a child class inherits attributes from its parent class. How about a class variable? Is a class variable in a parent class visible to its child class?

Yes, it is. Then, is a class variable in Child class visible to Parent class?

No, it is not. Let’s try a reader method for the class variable to see if a class variable reader method can be called on class Parent.

Still, this does not work. Is there any way a child can give something to its parent? Out of curiosity, is there a way for a parent class to use a class variable in a child class?

The coding works, but I guess it has no practical use.

--

--