简体   繁体   English

如何在Rails中访问ruby中的两个相同的类

[英]How to access two same classes in ruby on rails

I have class called Configuration that is called in my controller. 我在控制器中调用了名为Configuration的类。 Instead of instantiating my Configuration class, it is trying to instantiate a ActiveSupport::Configurable::Configuration . 而不是实例化我的Configuration类,它尝试实例化ActiveSupport::Configurable::Configuration

How can I access my Configuration class, named the same as another library's Configuration ? 如何访问我的 Configuration类,命名为另一个相同库的Configuration

class SampleController < ActionController::Base
  def m1
    cfg = Configuration.new
  end
end
class SampleController < ActionController::Base
  def m1
    cfg = ::Configuration.new
  end
end

I'd recommend putting your own classes in their own modules, though. 不过,我建议您将自己的类放在自己的模块中。

In addition to avoiding class name collisions like this, it provides a structure from which you can hang more of your own code. 除了避免像这样的类名冲突之外,它还提供了一个结构,您可以从中悬挂更多自己的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM