Last active
October 22, 2021 13:08
-
-
Save kazu69/a8563d70fdad6d453ffb to your computer and use it in GitHub Desktop.
ruby method overwrite (monkey patch pattern)
This file contains 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
class Country | |
def home | |
'Japan' | |
end | |
end | |
# ----- | |
module ExtendContry | |
def new_home | |
"Fukuoka Japan" | |
end | |
end | |
class Country | |
include ExtendContry | |
alias_method :old_home, :home | |
alias_method :home, :new_home | |
end | |
Country.new.home # => Fukuoka Japan | |
Country.new.old_home # => Japan | |
Country.ancestors | |
# => [Country, ExtendContry, Object, PP::ObjectMixin, Kernel, BasicObject] |
This file contains 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
class Country | |
def home | |
'Japan' | |
end | |
end | |
# ---- | |
class DelegateCountry < DelegateClass(Country) | |
def initialize(country) | |
super | |
end | |
def home | |
"Fukuoka #{super}" | |
end | |
end | |
contry = DelegateCountry.new(Country.new) | |
contry.home # => Fukuoka Japan |
This file contains 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
class Contry | |
def home | |
'Japan' | |
end | |
end | |
# ----- | |
module ExtendContry | |
def home | |
"Fukuoka #{super}" | |
end | |
end | |
class Country | |
include ExtendContry | |
end | |
Country.new.home # => Japan | |
Country.ancestors | |
# => [Country, ExtendContry, Object, PP::ObjectMixin, Kernel, BasicObject] |
This file contains 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
class Country | |
def home | |
'Japan' | |
end | |
end | |
# ----- | |
class Prefecture < Country | |
alias_method :old_home, :home | |
def home | |
"Fukuoka #{super}" | |
end | |
end | |
Prefecture.new.home # => Fukuoka Japan | |
Prefecture.new.old_home # => Japan | |
Prefecture.ancestors | |
# => [Prefecture, Country, Object, PP::ObjectMixin, Kernel, BasicObject] |
This file contains 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
class Country | |
def home | |
'Japan' | |
end | |
end | |
# ---- | |
class Prefecture | |
def home | |
method = Country.instance_method(:home) | |
"Fukuoka #{method.bind(Country.new).call()}" | |
end | |
end | |
Prefecture.new.home # Fukuoka Japan |
This file contains 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
class Country | |
def home | |
'Japan' | |
end | |
end | |
module ExtendCountry | |
def home | |
"Fukuoka #{super}" | |
end | |
end | |
class Prefecture < Country | |
prepend ExtendCountry | |
end | |
Prefecture.new.home # => Fukuoka Japan | |
Country.new.home # => Japan | |
refecture.ancestors | |
# => [ExtendCountry, Prefecture, Country, Object, PP::ObjectMixin, Kernel, BasicObject] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment