Skip to content

Instantly share code, notes, and snippets.

@akm
Last active June 20, 2020 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akm/fe9c2ced02b894cec9ebee00bb0dbac1 to your computer and use it in GitHub Desktop.
Save akm/fe9c2ced02b894cec9ebee00bb0dbac1 to your computer and use it in GitHub Desktop.
prependでモジュールを拡張する例
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