[英]How to call an instance method defined in a module that is overwritten by another module in Ruby?
[英]How to call a method defined in another module in Ruby?
class Foo
include Bar
include Baz
end
module Bar
def do_something
end
end
module Baz
do_something
end
Baz
模塊無權訪問Bar
。 有沒有辦法在Bar
調用方法?
一種方法是使用Bar
擴展 Baz
,但我想將它們全部包含在Foo
。
如果出於某種原因您不想使用Bar
擴展 Baz
,則可以創建一個在Baz
內擴展 Bar
的對象 :
module Bar
def do_something
puts 42
end
end
module Baz
Object.new.extend(Bar).do_something # prints 42
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.