Created
February 25, 2019 10:20
-
-
Save gacha/cdac549f7242de2081f785c9acc13704 to your computer and use it in GitHub Desktop.
ruby prepend
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 Car | |
def info | |
# some long method | |
puts "Only some info" | |
end | |
end | |
module FullInfo | |
def enable | |
@show_full_info = true | |
self | |
end | |
def info | |
if @show_full_info | |
puts "Full info" | |
else | |
super | |
end | |
end | |
end | |
Car.prepend FullInfo | |
Car.new.enable.info | |
Car.new.info | |
puts Car.ancestors.inspect | |
## Output: | |
# | |
# Full info | |
# Only some info | |
# [FullInfo, Car, Object, Kernel, BasicObject] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment