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.to_a / include? / cover? / each

Range.to_a / include? / cover? / each

These are basic operation methods for Range objects. They allow you to convert a range to an array, check for element membership, and determine whether a value falls within the range.

Syntax

# Creating a Range object.
(start..stop)    # Range from start to stop, inclusive (includes the end).
(start...stop)   # Range from start to stop, exclusive (excludes the end).

# Convert to an array.
range.to_a

# Check whether a value is included (compares each element).
range.include?(value)
range.member?(value)  # Alias for include?

# Check whether a value falls within the range (uses comparison operators, faster).
range.cover?(value)

# Return the first or last element.
range.first
range.first(n)  # Returns the first n elements as an array.
range.last
range.last(n)   # Returns the last n elements as an array.

# Iterate over each element.
range.each { |element| process }

Method List

MethodDescription
to_aConverts all elements in the range to an array and returns it.
include?(val)Returns whether val is included in the range by comparing each element in order.
member?(val)An alias for include?.
cover?(val)Returns whether val is within the range using comparison operators. Faster because it does not enumerate elements.
firstReturns the first element of the range.
first(n)Returns the first n elements of the range as an array.
lastReturns the last element of the range (inclusive ranges only).
each { |e| }Passes each element of the range to the block and iterates.

Sample Code

# Create Range objects.
integer_range = (1..5)
exclusive_range = (1...5)

# Convert to an array with to_a.
puts integer_range.to_a.inspect   # [1, 2, 3, 4, 5]
puts exclusive_range.to_a.inspect # [1, 2, 3, 4] (5 is not included)

# Ranges also work with letters.
puts ("a".."e").to_a.inspect  # ["a", "b", "c", "d", "e"]

# Check membership with include?.
puts (1..10).include?(5)   # true
puts (1..10).include?(11)  # false

# cover? uses comparison operators (also works with floating-point values).
puts (1..10).cover?(5.5)     # true (include? may return false in this case)
puts ("a".."z").cover?("m")  # true

# Loop with each.
(1..3).each { |i| print "#{i} " }
puts  # 1 2 3

# first / last.
puts (1..10).first              # 1
puts (1..10).first(3).inspect   # [1, 2, 3]
puts (1..10).last               # 10
puts (1..10).last(3).inspect    # [8, 9, 10]

Notes

Ruby Range objects come in two forms: .. (inclusive end) and ... (exclusive end). You can create a range from any object that includes the Comparable module, not just integers — strings and dates work too.

Be aware of the difference between include? and cover?. Because include? compares each element in sequence, it may not behave as expected for floating-point values or dates between discrete steps. cover?, on the other hand, uses only the comparison operator (<=>), making it faster and well-suited for checking continuous value ranges.

Calling last on an exclusive range (...) returns the end value itself, not the element just before it. This can be confusing, so be careful.

For aggregation operations on ranges, see range.min / max / sum / step.

If you find any errors or copyright issues, please .