Skip to content

Instantly share code, notes, and snippets.

@ncancelliere
Last active December 19, 2015 03:38
Show Gist options
  • Save ncancelliere/5891047 to your computer and use it in GitHub Desktop.
Save ncancelliere/5891047 to your computer and use it in GitHub Desktop.
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