Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

416
Vistas
Ruby's array min returning value, not array

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda