Using Instance Variables in Class Methods - Ruby

后端 未结 2 984
时光说笑
时光说笑 2020-12-04 19:15

I have a class something like below, and I used instance variables (array) to avoid using lots of method parameters.

It works as I expected but is that a good pract

2条回答
  •  孤城傲影
    2020-12-04 19:32

    Those are class instance variables and are a perfectly legitimate things in ruby: classes are objects too (instances of Class) and so have instance variables.

    One thing to look out for is that each subclass will have its own set of class instance variables (after all these are different objects): If you subclassed DummyClass, class methods on the subclass would not be able to see @arr.

    Class variables (@@foo) are of course the other way round: the entire class hierarchy shares the same class variables.

提交回复
热议问题