Last active
October 25, 2017 14:44
-
-
Save eprothro/cd64d4d63609e232ee702d62a433d9f2 to your computer and use it in GitHub Desktop.
You can temporarily override (not overwrite) a ruby method for an object by defining a method on the object metaclass.
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
| class Foo | |
| def bar | |
| :object_method | |
| end | |
| end | |
| f = Foo.new | |
| f.bar | |
| # => :object_method | |
| def f.bar | |
| :object_metaclass_method | |
| end | |
| f.bar | |
| # => :object_metaclass_method | |
| class << f | |
| remove_method(:bar) | |
| end | |
| f.bar | |
| # => :object_method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment