Constants or class variables in ruby?

后端 未结 5 1891
闹比i
闹比i 2020-12-30 18:37

I\'ve been programming in Ruby for a few months now, and I\'m wondering when it is appropriate to use constants over class variables and vice versa. (I\'m working in Rails,

5条回答
  •  悲哀的现实
    2020-12-30 19:12

    Note that class variables are private to a class and its instances (cf. http://phrogz.net/programmingruby/tut_classes.html). However, if you want to make your constant private you can always do:

    FOO = 18
    private_constant :FOO
    

提交回复
热议问题