Module: Tins::RangePlus

Defined in:
lib/tins/range_plus.rb

Overview

Note:

This implementation converts both ranges to arrays and concatenates

RangePlus extends the Range class with additional functionality.

This module adds a ‘+` method to Range objects that concatenates the elements of two ranges into a single array.

them. This means it will materialize the entire range into memory, which could be problematic for very large ranges.

Examples:

Basic usage

range1 = (1..3)
range2 = (4..6)
result = range1 + range2
# => [1, 2, 3, 4, 5, 6]

With different range types

range1 = ('a'..'c')
range2 = ('d'..'f')
result = range1 + range2
# => ["a", "b", "c", "d", "e", "f"]

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Array

Concatenates two ranges by converting them to arrays and merging them.

This method allows you to combine the elements of two ranges into a single array. Both ranges are converted to arrays using their ‘to_a` method, then concatenated together.

Examples:

(1..3) + (4..6)  # => [1, 2, 3, 4, 5, 6]

Parameters:

  • other (Range)

    Another range to concatenate with this range

Returns:

  • (Array)

    A new array containing all elements from both ranges



33
34
35
# File 'lib/tins/range_plus.rb', line 33

def +(other)
  to_a + other.to_a
end