Created
April 19, 2012 14:38
-
-
Save karledurante/2421364 to your computer and use it in GitHub Desktop.
Alternative to alias_method_chain
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 | |
# (A) original functionality | |
end | |
end | |
module PreExtension | |
def my_method | |
# (B) before the original | |
super # calls whatever was my_method before this definition was made | |
end | |
end | |
module PostExtension | |
def my_method | |
super # calls whatever was my_method before this definition was made | |
# (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 | |
#=> this is a twice extended method call that will execute code in this order: | |
#=> (B) before the original | |
#=> (A) the original | |
#=> (C) after the original |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment