Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Range.new

[out E] (E from, E to, ?::boolish exclude_end) -> ::Range[E]
Range.new(begin, end, exclude_end = false) → new_range

Returns a new range based on the given objects begin and end. Optional argument exclude_end determines whether object end is included as the last object in the range:

Example
Range.new(2, 5).to_a            # => [2, 3, 4, 5]
Range.new(2, 5, true).to_a      # => [2, 3, 4]
Range.new('a', 'd').to_a        # => ["a", "b", "c", "d"]
Range.new('a', 'd', true).to_a  # => ["a", "b", "c"]
See also #% #== also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0