[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array#last_index

jzakiya

4/9/2005 4:45:00 AM

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

18 Answers

Trans

4/9/2005 4:19:00 PM

0

Jabari,

I agree that would good to have. Also I would prefer the method name to
be very short. Do you have any ideas for this?

T.

Florian Groß

4/9/2005 4:26:00 PM

0

jzakiya@mail.com wrote:

> 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.

Can you not just use -1 as the index?



dblack

4/9/2005 4:28:00 PM

0

Bill Atkins

4/9/2005 4:29:00 PM

0

I don't think this should be all that slow. Ruby tracks the array's length
as it grows, so all you're doing here is reading a variable from memory and
subtracting one. Still, it would probably be a good idea to add this to
Array.

On Apr 9, 2005 12:49 AM, jzakiya@mail.com <jzakiya@mail.com> wrote:
>
> 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
>
>


--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"

Dominik Bathon

4/9/2005 5:43:00 PM

0

On Sat, 09 Apr 2005 18:26:15 +0200, Florian Groß <florgro@gmail.com> wrote:

> jzakiya@mail.com wrote:
>
>> 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.
>
> Can you not just use -1 as the index?

Here are some examples (in case you don't know how that works):

irb(main):083:0> [1, 2, 3, 4, 5][-1]
=> 5
irb(main):084:0> [1, 2, 3, 4, 5][-2]
=> 4
irb(main):085:0> [1, 2, 3, 4, 5][2..-1]
=> [3, 4, 5]
irb(main):086:0> [1, 2, 3, 4, 5][2..-2]
=> [3, 4]

And so on.

Dominik


Trans

4/9/2005 7:15:00 PM

0

> Can you not just use -1 as the index?

No. Because, it is not always simply a matter of getting to the last
element. If it were then #last itself would suffice. The use case here
is when the last index is needed.

T.

jzakiya

4/10/2005 5:00:00 AM

0

David A. Black wrote:
> Hi --
>
> On Sun, 10 Apr 2005, Trans wrote:
>
> > Jabari,
> >
> > I agree that would good to have. Also I would prefer the method
name to
> > be very short. Do you have any ideas for this?
>
> a.size-1 :-)
>
>
> David
>
> --
> David A. Black
> dblack@wobblini.net

These maybe: a.ndx a.endx a.lastndx a.last_i

I tend to like the last two the best because they are
closer to a.last_index, which is so POLS. ;-)

If not a.last_index I would go with a.last_i

Jabari

Trans

4/10/2005 4:41:00 PM

0

Thanks Jabari!

Array#last_index is now apart of Ruby Facets. Your suggestions for a
shorter name helped me figure out a nice alias too: #nth.

T.

ES

4/10/2005 5:36:00 PM

0


Le 10/4/2005, "Trans" <transfire@gmail.com> a écrit:

>Thanks Jabari!
>
>Array#last_index is now apart of Ruby Facets. Your suggestions for a
>shorter name helped me figure out a nice alias too: #nth.

The abbreviation 'nth' is often used to indicate a certain
unknown index position 'n' and may thus be confusing?

>T.

E

--
No-one expects the Solaris POSIX implementation!



Martin DeMello

4/10/2005 5:42:00 PM

0

Saynatkari <ruby-ml@magical-cat.org> wrote:
>
> Le 10/4/2005, "Trans" <transfire@gmail.com> a écrit:
>
> >Thanks Jabari!
> >
> >Array#last_index is now apart of Ruby Facets. Your suggestions for a
> >shorter name helped me figure out a nice alias too: #nth.
>
> The abbreviation 'nth' is often used to indicate a certain
> unknown index position 'n' and may thus be confusing?

I agree. Array#bound, perhaps, since the other bound is always zero
anyway.

martin