Ruby 中的类别名
我正在基于类似的现有应用程序开发一款新的 Rails 应用程序。在我的旧应用程序中,我有 Coupon 类,它与我的新应用程序中的 Ticket 非常相似。我想重用优惠券中的所有代码,但使用新的类名。
由于Rails中的重构很麻烦,我想知道是否有一种方法可以在Ruby中为类创建别名(类似于属性和方法的别名)。
I am developing a new Rails app based on a similar existing one. In my old app, I have Coupon class, which is very similar to Ticket in my new app. I want to reuse all code in Coupon, but with a new class name.
Since refactoring is cumbersome in Rails, I wonder if there is a way to create alias for a class in Ruby (similar to alias for attributes and methods).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Ruby 中的类没有名称。它们只是分配给变量的对象,就像任何其他对象一样。如果您想通过不同的变量引用一个类,请将其分配给不同的变量:
Classes don't have names in Ruby. They are just objects assigned to variables, just like any other object. If you want to refer to a class via a different variable, assign it to a different variable:
在文件 coupon.rb 中:
“任何以大写字母开头的引用,包括类和模块的名称,都是一个常量” -- << ruby 元编程>>,第 38 页,常量部分
in file coupon.rb:
"Any referrence that begins with an uppercase letter, including the names of classes and modules, is a constant" -- << metaprogramming ruby>>, page38, Constant section
任何来这里寻找如何为 Rails 模型类添加别名以获得新名称的人:
我可以简单地执行
Foo = Bar
,但必须将Foo
放入它自己的内部模型文件,这样我就不会收到未初始化的常量错误。例如,您可能会发现尝试在
has_many
、has_one
等关联中使用别名会很奇怪。我发现您通常可以通过使用根命名空间(或适当的命名空间(取决于模型的结构),以确保 Rails 尝试自动加载正确的常量:Anyone coming here looking for how to alias a rails model class to have a new name:
I was able to simply do
Foo = Bar
, but had to putFoo
inside it's own model file so that I wouldn't get a Uninitialized Constant Error. e.g.Also you may find weirdness trying to use the alias in associations like
has_many
,has_one
etc. I've found you can usually get around those by using the root namespace (or appropriate namespace depending on how your models are structured) to make sure Rails is trying to autoload the right constant:您必须小心这一点,因为如果您的类经历任何状态更改(添加函数、更改常量、类变量等),实例化别名时您的类所处的状态将不反映您班级的更新变化。
为了在不牺牲可读性的情况下避免腕管,您可以将 lambda 存储在别名对象中而不是实际的类中。当然,lambda 包含该类,但这可以确保您的别名将调用您的类的最新版本。
我将其放入我的
supermanpatches.rb
Rails 初始值设定项中(在config/initializers/
内部) ‡现在您可以使用
LAP[]
调用它并将加载您的类的新版本。 (允许您创建实例,例如,通过l = LAP[].new
)‡ 在 Rails 加载并运行时运行一次then 在您的应用程序中无处不在,可以在任何地方调用,就像全局变量一样,但是“只读”,可以这么说。
You've got to be careful with this, because if your class undergoes any state change (added functions, changed constants, class variables, etc) the state that your class was in when the alias was instantiated will not reflect the updated changes in your class.
In order to avoid carpal tunnel without sacrificing readability, you can store a lambda in your alias object rather than the actual class. Of course, the lambda contains the class but this assures your alias will call up the latest version of your class.
I put this in my
supermanpatches.rb
rails initializer (inside ofconfig/initializers/
) ‡Now you can call this using
LAP[]
and a freshly minted version of your class will be loaded. (Allowing you to create instances, for example, byl = LAP[].new
)‡ runs once when rails is loaded & then is pervasive through your app, callable anywhere kind of like a global variable but 'read-only', so to speak.
我或多或少同意warhog - 但我会从你的优惠券类别中子类门票 - 这样如果你需要进行任何数据修改,你可以将代码放入你的门票类别中
I agree with warhog, more or less - but I would subclass ticket from your coupon class - that way if you need to do any data munging, you can put the code in your ticket class