Create module variables in Ruby

前端 未结 4 623
闹比i
闹比i 2020-12-07 13:33

Is there any way to create a variable in a module in Ruby that would behave similar to a class variable? What I mean by this is that it would be able to be accessed without

4条回答
  •  佛祖请我去吃肉
    2020-12-07 14:27

    You can also initialize value within module definition:

    module MyModule
      class << self
        attr_accessor :my_variable
      end
      self.my_variable = 2 + 2
    end
    
    p MyModule.my_variable
    

提交回复
热议问题