Skip to content

Instantly share code, notes, and snippets.

@s-ashwinkumar
Last active August 21, 2017 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-ashwinkumar/6fdf4220bd3b8ef7c037861b4e38253f to your computer and use it in GitHub Desktop.
Save s-ashwinkumar/6fdf4220bd3b8ef7c037861b4e38253f to your computer and use it in GitHub Desktop.
A sample for alias method in ruby
class MyClass
def old_method
'old_method OR new_method'
end
alias :new_method :old_method
# NOTE-1 : 'alias' is a keyword and not a method, that is why
# there is no comma (I almost everytime type a comma there !)
# NOTE-2 : If you want to use the method kind of syntax you can use
# the 'method' Module#alias_method which does the same thing
# but is actually a method.
# so it would be 'alias_method :new_method, :old_method'
end
obj = MyClass.new
#=> #<MyClass:0x00000001859f48>
obj.old_method
#=> "old_method OR new_method"
obj.new_method
#=> "old_method OR new_method"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment