Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. Range.min / max / sum / count / step

Range.min / max / sum / count / step

Aggregation and step methods for Range objects. Use them to get the minimum, maximum, or sum of a range, or to iterate at a fixed interval.

Syntax

# Returns the minimum or maximum value.
range.min
range.max

# Specify a comparison criterion with a block.
range.min { |a, b| a <=> b }
range.max_by { |element| key }

# Returns the sum of all elements.
range.sum
range.sum { |element| transform }

# Returns the number of elements.
range.count
range.size  # Calculates quickly for integer ranges.

# Iterates at the specified interval.
range.step(interval) { |value| process }

Method List

MethodDescription
minReturns the minimum value in the range.
maxReturns the maximum value in the range (works correctly only for inclusive ranges).
min_by { |e| }Returns the element for which the block returns the smallest value.
max_by { |e| }Returns the element for which the block returns the largest value.
sumReturns the sum of all elements. If a block is given, each element is transformed before summing.
countReturns the total number of elements. You can also pass a block to count only elements matching a condition.
sizeReturns the number of elements in the range. For integer ranges, it calculates without enumerating elements.
step(n) { |v| }Iterates from the start value to the end in steps of n. Floating-point values are also supported.

Sample Code

range = (1..10)

# min / max: Get the minimum and maximum values.
puts range.min   # 1
puts range.max   # 10

# sum: Calculate the total (sum of 1 through 10 = 55).
puts range.sum   # 55

# Transform elements with a block before summing (sum of odd numbers only).
odd_total = range.sum { |n| n.odd? ? n : 0 }
puts odd_total   # 25 (1+3+5+7+9)

# count / size: Get the number of elements.
puts range.count  # 10
puts range.size   # 10

# Pass a block to count to filter by condition.
puts range.count { |n| n.even? }  # 5 (number of even values)

# step: Iterate at a specified interval.
(1..10).step(3) { |n| print "#{n} " }
puts  # 1 4 7 10

# Floating-point step.
(0.0..1.0).step(0.25) { |f| print "#{f} " }
puts  # 0.0 0.25 0.5 0.75 1.0

# Practical example: sum of squares from 1 to 100.
puts (1..100).sum { |n| n ** 2 }  # 338350

Notes

Range objects include the Enumerable module, so many of the same methods available on arrays work on ranges as well. min, max, sum, and count all come from Enumerable.

step can be applied not only to numeric ranges but also to date ranges (e.g., (Date.today..Date.today+7).step(1)). You can call it by passing a numeric argument directly or by using the block form.

Calling max on an exclusive range (...) returns the value just before the end. Make sure this is the maximum value you intended. Also, floating-point step may not reach the end value due to accumulated rounding errors.

For basic Range operations such as array conversion and membership checks, see Range#to_a / include? / cover?.

If you find any errors or copyright issues, please .