gpt4 book ai didi

ruby - ruby 模块

转载 作者:太空宇宙 更新时间:2023-11-03 17:06:00 25 4
gpt4 key购买 nike


module Hints
module Designer
def message
"Hello, World!"
end
end
end

p Hints::Designer.message

为什么这会给我以下错误...?

Hints::Designer:Module (NoMethodError) 的未定义方法“消息”

最佳答案

您需要做的是将消息定义为类方法(而不是实例方法)。尝试:

module Hints
module Designer
def self.message
"Hello, World!"
end
end
end

puts Hints::Designer.message

如果您想对模块使用实例方法,则必须使用模块和任何给定的约束来扩展对象(参见 http://ruby-doc.org/core/classes/Module.html)。

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

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