やったこと
refine を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
module-function.rb
class Sample
def hoge
"#{self.class.name}:#{__method__}"
end
end
module M
refine Sample do
def hoge2
"#{self.class.name}:#{__method__} 222"
end
end
end
p Sample.new.hoge
begin
p Sample.new.hoge2
rescue => e
p e
end
using M
p Sample.new.hoge2
出力結果
$ ruby refine.rb
"Sample:hoge"
#<NoMethodError: undefined method `hoge2' for #<Sample:0x00007fa29e0f1f48>
Did you mean? hoge>
"Sample:hoge2 222"