Method: Enumerable#min_by
- Defined in:
- enum.c
#min_by {|obj| ... } ⇒ Object #min_by ⇒ Object #min_by(n) {|obj| ... } ⇒ Array #min_by(n) ⇒ Object
Returns the object in enum that gives the minimum value from the given block.
If no block is given, an enumerator is returned instead.
a = %w(albatross dog horse)
a.min_by { |x| x.length } #=> "dog"
If the n argument is given, minimum n elements are returned as an array.
a = %w[albatross dog horse]
p a.min_by(2) {|x| x.length } #=> ["dog", "horse"]
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 |
# File 'enum.c', line 1781 static VALUE enum_min_by(int argc, VALUE *argv, VALUE obj) { struct MEMO *memo; VALUE num; rb_scan_args(argc, argv, "01", &num); RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size); if (!NIL_P(num)) return nmin_run(obj, num, 1, 0); memo = MEMO_NEW(Qundef, Qnil, 0); rb_block_call(obj, id_each, 0, 0, min_by_i, (VALUE)memo); return memo->v2; } |