I defined my own method to access elements as:
class Array2
def [](key)
if key.kind_of?(Integer)
@elements[key]
else
# ...
end
end
end
If I had previously declared @elements as Array.new, both the operations:
list = Array2.new
# ...
puts list[0]
puts list.[](0)
work properly. Why is the first operation acceptable?
Both the
list[0]andlist.[](0)syntaxes mean the exact same thing. They call the[]method with an argument0on thelistobject.