I came across the following while reading about how to easily override methods in Ruby:
class Array
alias :old_length :length
def length
old_length / 2
end
end
puts [1, 2, 3].length
Of course, this is a bad idea, but that makes sense. But it bothered me that we easily switch between :lengthand lengthand :old_lengthand old_length. So I tried it like this:
class Array
alias old_length length
def length
old_length / 2
end
end
puts [1, 2, 3].length
It works great - apparently, as in the first version. I feel that there is something obvious that I am missing, but I'm not sure what it is.
So, in nuthsell, why are :namethey nameinterchangeable in these cases?
source
share