Alias a old method a new name, then change the implementation of old method : alias « Method « Ruby






Alias a old method a new name, then change the implementation of old method


class Array
  alias :length_old :length
  def length
    return length_old / 2
  end
end

array = [1, 2, 3, 4]
array.length                                       # => 2
array.size                                         # => 4
array.length_old                                   # => 4

class Array
  alias :length :length_old
end

array.length                                       # => 4

 








Related examples in the same category

1.Give length a new name
2.Create a Wrapper method to alias
3.Redefining Backquotes
4.Aliasing Methods