Confusing behaviour of const_get in Ruby?

后端 未结 3 814
春和景丽
春和景丽 2021-02-01 05:59

According to the documentation mod.const_get(sym) \"Returns the value of the named constant in mod.\"

I also know that const_get by default may

3条回答
  •  名媛妹妹
    2021-02-01 06:24

    I came up with the following script to load name spaced constants:

    def load_constant(name)
      parts = name.split('::')
      klass = Module.const_get(parts.shift)
      klass = klass.const_get(parts.shift) until parts.empty?
      klass
    end
    

提交回复
热议问题