“个人”红宝石中的方法
我正在寻找一种使方法成为“个人”的方法 - 注意
这里是一个类的非私有示例 - 通过“个人”我的意思是方法“foo”的行为
class A
def foo
"foo"
end
end
class B < A
def foo
"bar"
end
end
class C < B
end
a=A.new; b=B.new;c=C.new
我正在寻找一种产生以下内容的方法行为
a.foo #=> "foo"
b.foo #=> "bar"
c.foo #=> "foo" (ultimate base class method called)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要创建“个人”方法,而是更改继承结构。
看来您希望 C 类仅具有 B 类的部分相同功能,而不对 A 类进行更改。
Instead of creating 'personal' methods, change your inheritance structure.
It appears that you want the C class to have only some of the same functionality of the B class while not making changes to the A class.
没有标准的方法可以做到这一点。它规避了继承的工作原理。您可以实现 B 的方法来执行如下逻辑:
您当然可以在 Class 上定义一个方法,该方法将为您执行类似于
public
和private
的操作。然后您可以在 B 中使用
personal :foo
,就像private :foo
一样。(这根本没有优化,而且我没有实现
public
和private
所具有的零参数行为,因为坦率地说,要做正确的事情是一个巨大的 PITA,即使这样这是一个黑客行为。)There's no standard way of doing this. It circumvents how inheritance works. You could implement B's method to do the logic like this:
And you could of course define a method on Class that would do this for you similar to
public
andprivate
.Then you can
personal :foo
in B just like you'dprivate :foo
.(This isn't at all optimized and I didn't implement the zero-argument behavior that
public
andprivate
have because frankly it's a huge PITA to do right and even then it's a hack.)看起来这可能会令人困惑,但这里有一个选择:
更一般地,使用类似的方法,您始终可以向给定实例添加一个方法,这当然对继承的类或同一类的其他实例没有影响。
如果您向我们提供您最终想要实现的目标的具体信息,我们可能会更有帮助。
Seems like it could be confusing, but here's one option:
More generally, using a similar approach, you can always add a method to a given instance, which of course has no effect on inherited classes or indeed on other instances of the same class.
If you give us specifics of what you're ultimately trying to accomplish we can probably be more helpful.
回答这个问题有点棘手,因为我真的不明白你想在实践中完成什么,但你可以尝试类似的方法
(
ancestors[-3]
假设 A 继承自 Object 和 Kernel,并且您的目的是从最顶层的非内置类访问该方法,当然您可以用A
替换self.class.ancestors[-3]
,或者弄清楚。 :实际上,如果您可以修改它,在类
B
中为原始类添加别名会更简单(即alias foo_from_A :foo
在class B 中,在新的
def foo
之前,那么你可以在C 中调用
foo_from_A
)。或者只是在C
中重新定义您想要的内容。或者以不同的方式设计整个类层次结构。Answering this is a bit tricky since I don't really see what you want to accomplish in practice, but you could try something like
(The
ancestors[-3]
assumes that A inherits from Object and Kernel and your intent was to access the method from the topmost non-builtin class. Of course you could substituteself.class.ancestors[-3]
with justA
, or figure out the class from the Arrayancestors
yourself, etc.)In practice it would be simpler to alias the original in class
B
if you can modify it (i.e.alias :foo_from_A :foo
inclass B < A
before the newdef foo
, then you can callfoo_from_A
inC
). Or just redefine what you want inC
. Or design the whole class hierarchy differently.您可以编写一个快捷函数来处理个性化方法。
You can write a shortcut function to handle personalizing methods.
有时您并不真正想要“是”(继承)关系。有时你想要的是“像嘎嘎一样”。通过使用模块“混合”方法,可以轻松地在“庸医”类之间共享代码:
Sometimes you don't really want an "is a" (inheritance) relationship. Sometimes what you want is "quacks like a." Sharing code among "quacks like a" classes is easily done by using modules to "mix in" methods: