J
jzakiya
array.last returns the value of the last array element.
In many instances it is necessary to get the index value
of the last array element. This is necessary in many
sorting and searching algorithms, especially where the
arrays are dynamically changing in size. You can do it like:
class Array
def last_index; self.length - 1 end
end
But this is slower than necessary, because array.last
already computes/knows what the last index value is,
and thus the mechanism already exists to get this info.
I would really like to see this method added to class Array.
It is a nice complement to Array#last, and aides performance.
Jabari Zakiya
In many instances it is necessary to get the index value
of the last array element. This is necessary in many
sorting and searching algorithms, especially where the
arrays are dynamically changing in size. You can do it like:
class Array
def last_index; self.length - 1 end
end
But this is slower than necessary, because array.last
already computes/knows what the last index value is,
and thus the mechanism already exists to get this info.
I would really like to see this method added to class Array.
It is a nice complement to Array#last, and aides performance.
Jabari Zakiya