gpt4 book ai didi

ruby - 稍后定义的类的模块实例方法

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

我想指出,这完全是一个虚构的问题。我知道还有其他方法可以做到这一点。

我想像这样声明一个模块

module Foo
# some logic here to
# get instance method 'foo' on
# a later defined class
end

然后我想声明一个类:

class Foo::Bar
end

然后不使用 include 或 extend 就可以做到这一点:

Foo::Bar.new.foo

并让它调用我在模块 Foo 中定义的 foo 方法

最佳答案

module Foo
class Bar
def foo
puts "erik is a dummy"
end
end
end
Foo::Bar.new.foo
=> erik is a dummy

关于ruby - 稍后定义的类的模块实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11253282/

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