How do I refer to a submodule's “full path” in ruby?

后端 未结 1 1687
情深已故
情深已故 2021-01-19 04:32

In my app, I have

VeryUniqueModule::Foo
# and…
VeryUniqueModule::Bar

Foo and Bar are each for a different service

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 05:22

    You might try this:

    VeryUniqueModule.const_get('::VeryUniqueModule::' + @relevant_object.service)
    

    And if that doesn't work, you could try bypassing service_api and doing this wherever you need A_CONSTANT:

    Object.const_get('::VeryUniqueModule::' + @relevant_object.service + '::A_CONSTANT')
    

    Note the :: before VeryUniqueModule. I don't think it's strictly necessary in this case, but it could be useful in that it guarantees Ruby will look for VeryUniqueModule in the global namespace and not inside some other module.

    0 讨论(0)
提交回复
热议问题