I came across the following while reading about how easy it is to redefine methods in Ruby:
class Array
alias :old_length :length
def length
old_length / 2
end
end
puts [1, 2, 3].length
Sure, it's a bad idea, but it makes the point. But it bothered me that we switch between :length
and length
and :old_length
and old_length
so easily. So I tried it this way:
class Array
alias old_length length
def length
old_length / 2
end
end
puts [1, 2, 3].length
It works just fine - apparently just like the first version. I feel like there's something obvious that I'm missing, but I'm not sure what it is.
So, in a nuthsell, why are :name
and name
interchangeable in these cases?