Create module variables in Ruby

前端 未结 4 627
闹比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:22

    you can set a class instance variable in the module.

    module MyModule
       class << self; attr_accessor :var; end
    end
    
    MyModule.var = 'this is saved at @var'
    
    MyModule.var    
    => "this is saved at @var"
    

提交回复
热议问题