views:

379

answers:

3

If a is the array, I want a.index(a.max), but something more Ruby-like. It should be obvious, but I'm having trouble finding the answer at so and elsewhere. Obviously, I am new to Ruby.

Cary

+13  A: 

For Ruby 1.8.7 or above:

a.each_with_index.max[1]

It does one iteration. Not entirely the most semantic thing ever, but if you find yourself doing this a lot, I would wrap it in an index_of_max method anyway.

Chuck
Wow. How does this do what it does?
Wayne Conrad
Agreed - how's this one work?
bergyman
Aaah, got it. each_with_index.max returns an array with the first element being the value and the second being the index of it. Very nice, Chuck.
bergyman
Although actually still foggy as to why exactly it returns an array like this...heh.
bergyman
Chuck, I was aware of this method, but thought Ruby would have a way of returning just the index. (For any others new to Ruby, a.each_with_index.max returns the array [max value, index of max value], so Chuck is just pulling out the second element.)
Cary Swoveland
`each_with_index` without a block returns an enumerator that gives the item and its index. We then send `max` to this enumerator, which does the standard `max` algorithm on item-index pairs. `Array.<=>` is implemented so that the first item determines the ordering (unless there's a tie, in which case the second is compared, and so on), so this works basically the same as doing `max` on an array of the values themselves. Then to get the index, we ask for the second item of the result (since we got a series of `[value, index]` pairs from `each_with_index`).
Chuck
Boy, so is fast! The five comments before mine were posted while I was composing my comment.
Cary Swoveland
Nice. I was unaware that doing a max on a 2D array like this just compares the value of the first element in each array.
bergyman
That's a great way of doing it.If using Ruby 1.8.6, `require 'backports'` to get `#each_with_index`
Marc-André Lafortune
A: 
a = [1, 4 8]
a.inject(a[0]) {|max, item| item > max ? item : max }

At least it's Ruby-like :)

Jarrett Meyer
Dammit! I was cooking up a solution using inject - you beat me to it! ;)
bergyman
Also - original question was to get the index, so this would have to be changed to: a.inject(0) {|index, num| num > a[index] ? a.find_index(num) : index}
bergyman
A: 

If you do an array.sort!, then the largest element will be the last one (array.last) and will have index array.length-1.

bta
I expect there was no expressed support for your solution merely because sorting an array is an inefficient way of finding the max.
Cary Swoveland
I agree, sorting an array just to find the max is inefficient. I mentioned it because I didn't know what else was being done with the array, so I thought I'd mention it in case the OP was going to sort it anyway for processing.
bta