How can I use the alias_method method in Ruby to make a copy of the [] method in the Array class
create aliases for a method and variable name in ruby. This can
be helpful if you want to override the behavior of some method
without changing the origin implementation of it.
alias_method
take a new_name
as a copy
name of the old_name
and it has the following
syntax.
Syntax :
alias_method (new_name, old_name)
Example
class Davi
def capital puts "Karaz-a-Karak"
end
alias_method :orig_capital, :capital
def capital puts "Karaz-a-Karak rebuild"
orig_capital
end
end
davi = Davi.new
davi.capital
Get Answers For Free
Most questions answered within 1 hours.