module Foo
def bar
puts 'Foo Bar!'
end
end
class Person
def bar
puts 'Person Bar!'
end
end
class Person
# I want this line to raise an error because Foo and Person has same-named methods that would break dependencies if ignored
include Foo
end
Person.new.bar
# => Person Bar!
include
和extend
的替代方法,除了在已经定义包含或扩展方法时会引发错误。{ LI>
include!
或extend!
吗?我正在编写一个小型gem,它将向Object
类添加方法:
class Object
include MyGemName
end
,我想首先确保Object
类尚未定义同名方法。
class Person
duplicate_methods = self.instance_methods(false) & Foo.instance_methods(false)
raise "#{self} already has methods: #{duplicate_methods}" unless duplicate_methods.empty?
include Foo
end
注意:
我需要覆盖Object
类,因为我想跳过将self
作为参数传递给所述方法。
我可能在这里只是简单的遗漏,但我在互联网上的搜索没有给我带来任何有用的解决方案。也许那时候,我做错了什么?
答案 0 :(得分:4)
如果确实想要这种行为,您可以考虑以下事项:
const HelloWorld = ({name}) => (
<div>
{`Hi ${name}`}
<Helmet>
<title>Who's Marco?</title>
<Helmet>
</div>
);
运行代码会得到以下结果:
在`included&#39;:Foo覆盖bar(RuntimeError)