module Ext
refine Hash do
def foo
puts :in_foo
end
def bar
puts :in_bar
foo
end
end
end
module Test
using Ext
Hash.new.bar
end
# in_bar
# in_foo
# => nil
这按预期工作。但是,如果我想在和之间共享foo
和bar
使用Hash
,Array
它include
会失败。
module Shared
def foo
puts :in_foo
end
def bar
puts :in_bar
foo
end
end
module Ext
refine Hash do
include Shared
end
refine Array do
include Shared
end
end
module Test
using Ext
Hash.new.bar
end
# in_bar
# NameError: undefined local variable or method `foo' for {}:Hash
有没有办法在改进之间共享代码?