Method: Enumerable#minmax
- Defined in:
- enum.c
#minmax ⇒ Array #minmax {|a, b| ... } ⇒ Array
Returns two elements array which contains the minimum and the maximum value in the enumerable. The first form assumes all objects implement Comparable; the second uses the block to return a <=> b.
a = %w(albatross dog horse)
a.minmax #=> ["albatross", "horse"]
a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 |
# File 'enum.c', line 1440 static VALUE enum_minmax(VALUE obj) { NODE *memo = NEW_MEMO(Qundef, Qundef, Qundef); struct minmax_t *m = (struct minmax_t *)&memo->u1.value; VALUE ary = rb_ary_new3(2, Qnil, Qnil); m->min = Qundef; m->last = Qundef; if (rb_block_given_p()) { rb_block_call(obj, id_each, 0, 0, minmax_ii, (VALUE)memo); if (m->last != Qundef) minmax_ii_update(m->last, m->last, m); } else { rb_block_call(obj, id_each, 0, 0, minmax_i, (VALUE)memo); if (m->last != Qundef) minmax_i_update(m->last, m->last, m); } if (m->min != Qundef) { rb_ary_store(ary, 0, m->min); rb_ary_store(ary, 1, m->max); } return ary; } |