Skip to content

Instantly share code, notes, and snippets.

@karledurante
Created April 19, 2012 14:38
Show Gist options
  • Save karledurante/2421364 to your computer and use it in GitHub Desktop.
Save karledurante/2421364 to your computer and use it in GitHub Desktop.
Alternative to alias_method_chain
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