1

In Ruby (3.0.1) the min function on an array

Returns one of the following:

  • The minimum-valued element from self.
  • A new Array of minimum-valued elements selected from self.

(from here).

So, given

l = [{n: 1, m: 6}, {n: 1, m: 5}, {n: 2, m: 4}, {n: 3, m: 3}, {n: 4, m: 3}]

I would expect

l.min { |a, b| a[:n] <=> b[:n] }
=> [{:n=>1, :m=>6}, {:n=>1, :m=>5}]

but instead I get

l.min { |a, b| a[:n] <=> b[:n] }
=> {:n=>1, :m=>6}

Why? Why am I getting one of the list of the minimal elements rather than the entire list of minimal elements?

dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • When using `min` or `min_by`, if you want to return an array of all elements that are tied for having the min value, you must first determine the min value and then use `select` to select all elements having that value. Similar for `max` and `max_by`. – Cary Swoveland Dec 15 '21 at 18:33

1 Answers1

3

If you read the rest of the specification:

With no argument and (a block/no block), returns the element in self having the minimum value per (the block/method <=>):

The only case when it returns more than one element is if you specify the number of elements that you want returned:

With an argument n and (a block/no block), returns a new Array with at most n elements, in ascending order per (the block/method <=>):

[0, 1, 2, 3].min(3) # => [0, 1, 2]
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • 1
    In any case, if there are multiple elements which compare the same according to the defined block (i.e. elements with the same `n` value in the question here), it is undefined which of those specific elements is returned. – Holger Just Dec 15 '21 at 16:23
  • @HolgerJust, if I need a stable ordering, such that array.min { block } always returns the same element, is it relatively safe to assume that in the case of a tie the first will be returned? – dumbledad Dec 21 '21 at 09:45
  • No, this is not specified and can not be relied on. In the case of a tie, any one of the possible elements can be returned. If a stable sort is important for you, you should introduce an additional/secondary sort criterium, e.g. `l.min { |a, b| [a[:n], a[:m]] <=> [b[:n], b[:m]] }` or equivalently `l.min_by { |e| [e[:n], e[:m]] }`. – Holger Just Dec 21 '21 at 11:01