How to make instance variables private in Ruby?

后端 未结 6 1235
孤城傲影
孤城傲影 2020-12-04 17:52

Is there any way to make instance variables \"private\"(C++ or Java definition) in ruby? In other words I want following code to result in an error.

class B         


        
6条回答
  •  不思量自难忘°
    2020-12-04 18:37

    It isn't possible to do what you want, because instance variables aren't defined by the class, but by the object.

    If you use composition rather than inheritance, then you won't have to worry about overwriting instance variables.

提交回复
热议问题