Created
June 30, 2009 13:28
-
-
Save fronx/138155 to your computer and use it in GitHub Desktop.
how to overwrite mixed-in methods while still having access to the original implementation via super
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
module X | |
def x | |
"x" | |
end | |
module_function :x | |
def self.included(base) | |
base.class_eval do | |
public :x | |
end | |
end | |
end | |
class TestDefault | |
include X | |
end | |
puts TestDefault.new.x | |
# => x | |
module OverwriteX | |
include X | |
def x | |
"#{super}2" | |
end | |
end | |
class TestOverwrite | |
include OverwriteX | |
end | |
puts TestOverwrite.new.x | |
# => x | |
module DontOverwriteX | |
include X | |
end | |
class TestDontOverwrite | |
include DontOverwriteX | |
end | |
puts TestDontOverwrite.new.x | |
# => x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment