From: Tanaka Akira Date: 2013-09-10T23:48:39+09:00 Subject: [ruby-core:57113] Re: [ruby-trunk - Feature #8887] min(n), max(n), min_by(n), max_by(n) 2013/9/10 Hanmac (Hans Mackowiak) : > Issue #8887 has been updated by Hanmac (Hans Mackowiak). > hm i am curios in what order max(n) and max_by(n) should return the elements? > > like [6, 0, 3, 3, 8, 3, 5, 0, 6].max(4): > should it return sort.last(n) #> [5, 6, 6, 8] > or is that better? [8, 6, 6, 5] because 8 is bigger than 6 ? I think sorted order (minimum element first, maximum element last) is intuitive. > PS: min_by(n) and max_by(n) (and maybe sort_by(n)) should maybe implemented different without take, so that it max_by(n) picks only the first n biggest objects without sorting the entire test of the Enumerator? The methods accumelates 4n elements, "sort" them and drop 3n elements repeatedly. Also, the "sort" is based on quick sort but doesn't sort a half of divided elements. > also about your patch, e.min_by(n).size should respect also n and also enum_size, so it needs to return n when n < as size, and size when n > size (for size != nil) As far as I understand, the size method returns number of yield invocaitons of the enumerator. The enumerators, e.min_by(n) and e.max_by(n), yields e.size times. So they returns e.size which is not related to n. For example, e.reject.size returns e.size as follows. (1..10).to_a.reject.size #=> 10 This doesn't mean the result array size of reject method. -- Tanaka Akira