How to I make private class constants in Ruby

前端 未结 4 1252
执笔经年
执笔经年 2021-02-01 12:49

In Ruby how does one create a private class constant? (i.e one that is visible inside the class but not outside)

class Person
  SECRET=\'xxx\' # How to make clas         


        
4条回答
  •  青春惊慌失措
    2021-02-01 13:36

    You can also change your constant into a class method:

    def self.secret
      'xxx'
    end
    
    private_class_method :secret
    

    This makes it accessible within all instances of the class, but not outside.

提交回复
热议问题