0

Hi I have two modules

  1. Admin
  2. Blog (Blog is a rails engine) where Admin is a module for namespacing admin features of app, but Blog is a module representing rails engine. Is there a better way to determine which among them is engine, like a function "is_engine?"

Admin.is_engine?

=> false

Blog.is_engine?

=> true

Definately I can have a try catch thing to determine this

def is_engine? module
  module::Engine
  true
rescue NameError
  false
end

here

is_engine? Admin

will return false

is_engine? Blog

will return true

Thanks

4

1 回答 1

3

我不确定我是否理解您要做什么:Rails Engine 是一个类(的子类Rails::Engine),而不是模块。

如果你有一个实例,你可以使用:

admin.kind_of?(Rails::Engine)

如果你有一个,你可以使用:

Something.ancestors.include?(Rails::Engine)

如果你拥有的是一个模块,那么它就不能是 Rails::Engine 的子类,也不是引擎。

编辑

如果您有一个模块或常量something,并且想知道其命名空间中是否有一个具有特定名称的常量,您可以使用:

something.constants.include?(:Engine)
于 2013-04-30T08:09:15.827 回答