Skip to content

Instantly share code, notes, and snippets.

@bachue
Last active December 10, 2015 18:08
Show Gist options
  • Save bachue/4472791 to your computer and use it in GitHub Desktop.
Save bachue/4472791 to your computer and use it in GitHub Desktop.
原来Ruby的module里的方法都能继承哦 Cool~
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