Last active
December 19, 2015 03:38
-
-
Save ncancelliere/5891047 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 say_hello | |
puts 'Hello Nick!' | |
end | |
end | |
# Reopen the class to override the say_hello method | |
MyClass.class_eval do | |
# alias_method creates a *copy* of the method object, it is not really a pointer (or alias) | |
alias_method :say_hello_original, :say_hello | |
# here we override and then call original method | |
def say_hello | |
puts 'I am the evil Nick!' | |
say_hello_original | |
end | |
end | |
x = MyClass.new | |
x.say_hello | |
# I am the evil Nick! | |
# Hello Nick! <- proof that the alias_method is making a copy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment