Last active
December 10, 2015 18:08
-
-
Save bachue/4472791 to your computer and use it in GitHub Desktop.
原来Ruby的module里的方法都能继承哦 Cool~
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
class Something | |
module Base | |
def my_method | |
puts "(A) original functionality" | |
end | |
end | |
module PreExtension | |
def my_method | |
puts "(B) before the original" | |
super | |
end | |
end | |
module PostExtension | |
def my_method | |
super | |
puts "(C) after the original" | |
end | |
end | |
include Base # this is needed to place the base methods in the inheritance stack | |
include PreExtension # this will override the original my_method | |
include PostExtension # this will override my_method defined in PreExtension | |
end | |
s = Something.new | |
s.my_method | |
# => (B) before the original | |
# => (A) original functionality | |
# => (C) after the original |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment