gpt4 book ai didi

ruby - 包括两个具有相同方法名称的 Ruby mixins

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:14 24 4
gpt4 key购买 nike

这是怎么回事:

module Sounds
def dog
"bark"
end
end

module Noises
def dog
"woof"
end
end

class Animals
include Sounds
include Noises
end

x = Animals.new
x.dog # Returns "woof", as I expected

class Animals
include Sounds
end

x.dog # Still returns "woof" for some reason -- shouldn't it be bark?

y = Animals.new
y.dog # Also returns "woof" for some reason -- shouldn't it be bark?

最佳答案

一旦您包含了一个模块,我不确定它是否会再次包含。它可能会被列为已包含,因此重复操作将被忽略。

如果您需要这样做,这确实很奇怪,您可能需要通过制作一个包含目标模块的模块(即使是临时模块)来伪造 Ruby,然后改为包含该模块。

关于ruby - 包括两个具有相同方法名称的 Ruby mixins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12024537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com