gpt4 book ai didi

ruby - Ruby 中的模块方法

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

这是我的代码:

module Star
def Star.line
puts '*' * 20
end
end

module Dollar
def Star.line
puts '$' * 20
end
end

module At
def line
puts '@' * 20
end
end

include At
Dollar::line # => "@@@@@@@@@@@@@@@@@@@@"
Star::line # => "$$$$$$$$$$$$$$$$$$$$"
Dollar::line # => "@@@@@@@@@@@@@@@@@@@@"
line # => "@@@@@@@@@@@@@@@@@@@@"

谁能解释一下我是如何得到这个结果的?我不明白这里的方法查找流程。

最佳答案

我是这样看的:

美元::线

这个模块中没有定义这样的方法,所以它调用 At::line 因为你包含了这个模块。

星::线

它使用来自 Dollar 模块的最后定义(它遵循原始 Star 定义,因此它被覆盖)。

美元::线

第三次调用与第一次相同。

最后一个是 At::line 因为你做了一个包含。

关于ruby - Ruby 中的模块方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31319472/

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