Ruby on Rails: Where to define global constants?

前端 未结 12 1762
我在风中等你
我在风中等你 2020-11-28 00:44

I\'m just getting started with my first Ruby on Rails webapp. I\'ve got a bunch of different models, views, controllers, and so on.

I\'m wanting to find a good plac

12条回答
  •  温柔的废话
    2020-11-28 01:28

    Another option, if you want to define your constants in one place:

    module DSL
      module Constants
        MY_CONSTANT = 1
      end
    end
    

    But still make them globally visible without having to access them in fully qualified way:

    DSL::Constants::MY_CONSTANT # => 1
    MY_CONSTANT # => NameError: uninitialized constant MY_CONSTANT
    Object.instance_eval { include DSL::Constants }
    MY_CONSTANT # => 1
    

提交回复
热议问题