Method: Range#min

Defined in:
range.c

#minObject #min(n) ⇒ Array #min {|a, b| ... } ⇒ Object #min(n) {|a, b| ... } ⇒ Array

Returns the minimum value in self, using method #<=> or a given block for comparison.

With no argument and no block given, returns the minimum-valued element of self.

(1..4).min     # => 1
('a'..'d').min # => "a"
(-4..-1).min   # => -4

With non-negative integer argument n given, and no block given, returns the n minimum-valued elements of self in an array:

(1..4).min(2)     # => [1, 2]
('a'..'d').min(2) # => ["a", "b"]
(-4..-1).min(2)   # => [-4, -3]
(1..4).min(50)    # => [1, 2, 3, 4]

If a block is given, it is called:

  • First, with the first two element of self.

  • Then, sequentially, with the so-far minimum value and the next element of self.

To illustrate:

(1..4).min {|a, b| p [a, b]; a <=> b } # => 1

Output:

[2, 1]
[3, 1]
[4, 1]

With no argument and a block given, returns the return value of the last call to the block:

(1..4).min {|a, b| -(a <=> b) } # => 4

With non-negative integer argument n given, and a block given, returns the return values of the last n calls to the block in an array:

(1..4).min(2) {|a, b| -(a <=> b) }  # => [4, 3]
(1..4).min(50) {|a, b| -(a <=> b) } # => [4, 3, 2, 1]

Returns an empty array if n is zero:

(1..4).min(0)                      # => []
(1..4).min(0) {|a, b| -(a <=> b) } # => []

Returns nil or an empty array if:

  • The begin value of the range is larger than the end value:

    (4..1).min                         # => nil
    (4..1).min(2)                      # => []
    (4..1).min {|a, b| -(a <=> b) }    # => nil
    (4..1).min(2) {|a, b| -(a <=> b) } # => []
    
  • The begin value of an exclusive range is equal to the end value:

    (1...1).min                          # => nil
    (1...1).min(2)                       # => []
    (1...1).min  {|a, b| -(a <=> b) }    # => nil
    (1...1).min(2)  {|a, b| -(a <=> b) } # => []
    

Raises an exception if either:

  • self is a beginless range: (..4).

  • A block is given and self is an endless range.

Related: Range#max, Range#minmax.

Overloads:



1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
# File 'range.c', line 1597

static VALUE
range_min(int argc, VALUE *argv, VALUE range)
{
    if (NIL_P(RANGE_BEG(range))) {
        rb_raise(rb_eRangeError, "cannot get the minimum of beginless range");
    }

    if (rb_block_given_p()) {
        if (NIL_P(RANGE_END(range))) {
            rb_raise(rb_eRangeError, "cannot get the minimum of endless range with custom comparison method");
        }
        return rb_call_super(argc, argv);
    }
    else if (argc != 0) {
        return range_first(argc, argv, range);
    }
    else {
        VALUE b = RANGE_BEG(range);
        VALUE e = RANGE_END(range);
        int c = NIL_P(e) ? -1 : OPTIMIZED_CMP(b, e);

        if (c > 0 || (c == 0 && EXCL(range)))
            return Qnil;
        return b;
    }
}