Ruby已经在使用我的模型的类名 [英] Ruby is already using the class name of my model

查看:134
本文介绍了Ruby已经在使用我的模型的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作一个具有各种权限级别的论坛应用程序,其中一个是监视器。我这样做是通过扩展我的User类,我计划用-ship类(例如管理,作者,协调等)微调。显然,Monitor类是ruby的一部分 mixin

I'm making a forum application with various levels of authorization, one of which is a Monitor. I am doing this by extending my User class, and I plan on fine tuning this with "-ship" classes (e.g. administratorship, authorship, moderatorship, etc.). Apparently the Monitor class is part of ruby mixin. How do I keep my resource name without the collisions?

推荐答案

一些可能性:


  • 避免引入标准Monitor实例的 require'monitor.rb'调用

  • 做一些运行时魔法重命名现有的Monitor类。

  • 使用您的加载路径,使 require'monitor.rb'拉入一个空的实现Monitor。

  • avoid the require 'monitor.rb' call which is pulling in the standard Monitor instance
  • do some runtime magic to rename the existing Monitor class.
  • monkey with your load path so that require 'monitor.rb' pulls in an empty implementation of Monitor.

但在所有情况下,您可能会遇到第三方库使用Monitor期望它是标准Monitor类。

But in all cases you could end up with the situation where a 3rd party library is using Monitor expecting it to be the standard Monitor class. So, I'd advise against any of the above.

我会说你唯一两个合理的选择是:

I'd say your only two reasonable options are:

A)您可以将您的类放在命名空间中:

A) you could put your class in a namespace:

Module MyApp
  class Monitor
    #...
  end
end

如果您的应用程式使用某种自动需要魔法(例如它是一个rails应用程序),那么你会把你的实现在/m​​y_app/monitor.rb。当你想引用这个类时,你会做一些 my_monitor = MyApp :: Monitor.new(),或者其他。

if your app uses some kind of auto-require magic (e.g it's a rails app) then you would put your implementation in /my_app/monitor.rb. When you wanted to refer to that class you would do something like my_monitor = MyApp::Monitor.new(), or whatever.

B)你可以使用不同的类名:)

B) you could use a different class name :)

这篇关于Ruby已经在使用我的模型的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆