Last active
June 20, 2020 02:00
-
-
Save akm/fe9c2ced02b894cec9ebee00bb0dbac1 to your computer and use it in GitHub Desktop.
prependでモジュールを拡張する例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Bar | |
def bar | |
"bar" | |
end | |
end | |
class Foo | |
include Bar | |
end | |
foo1 = Foo.new | |
puts foo1.bar | |
module Baz | |
def bar_with_patch | |
"#{bar_without_patch} with patch" | |
end | |
def self.prepended(mod) | |
mod.module_eval do | |
alias_method :bar_without_patch, :bar | |
alias_method :bar, :bar_with_patch | |
end | |
end | |
end | |
Bar.prepend(Baz) | |
puts foo1.bar | |
foo2 = Foo.new | |
puts foo2.bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment