Ruby中的类别名 [英] Class alias in Ruby
问题描述
我正在开发一个基于类似的现有Rails应用程序。在我的旧应用程序,我有Coupon类,这是非常相似的Ticket在我的新应用程序。我想重用所有代码在Coupon,但有一个新的类名。
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.
由于重构在Rails是繁琐的,我想知道是否有一种方法来创建别名为类别在Ruby中(类似于属性和方法的别名)。
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).
推荐答案
类在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:
Foo = String
这篇关于Ruby中的类别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!