Skip to content

Instantly share code, notes, and snippets.

@mark
Created August 20, 2015 15:14
Show Gist options
  • Save mark/0dd1adac025b6bbb0d93 to your computer and use it in GitHub Desktop.
Save mark/0dd1adac025b6bbb0d93 to your computer and use it in GitHub Desktop.
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
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