Created
August 20, 2015 15:14
-
-
Save mark/0dd1adac025b6bbb0d93 to your computer and use it in GitHub Desktop.
This file contains 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 MyClass | |
def foo | |
puts "MyClass -> foo" | |
bar | |
end | |
def bar | |
puts "MyClass -> bar" | |
end | |
end | |
class YourClass < MyClass | |
def bar | |
puts "YourClass -> bar" | |
end | |
end | |
class MyWrapper < Struct.new(:obj) | |
def foo | |
@obj.foo | |
end | |
def bar | |
puts "MyWrapper -> bar" | |
end | |
end |
This file contains 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
myClass = MyClass.new | |
myClass.bar | |
#=> "MyClass -> bar" | |
yourClass = YourClass.new | |
yourClass.bar | |
#=> "YourClass -> bar" | |
myWrap = MyWrapper.new(myClass) | |
myWrap.bar | |
#=> "MyWrapper -> bar" | |
# So far, so good. | |
yourClass.foo | |
#=> "MyClass -> foo" | |
#=> "YourClass -> bar" | |
# So it calls the original foo, and then my overridden bar. Good. | |
myWrap.foo | |
#=> "MyClass -> foo" | |
#=> "MyClass -> bar" | |
# But we're trying to replace the original bar, but anything that calls into it hits | |
# the original, not our wrapped version. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment