in Ruby, how do I push elements to the end of a range?

ruby

Solution

A range is simply an interval. A start and an end. You don't just push values on the end of a Range. You either convert the Range to an array

items = range.to_a
items << 11

Or you create a new range..

Range.new(range.begin, 11)

EDIT: The reason we use #begin on the range and not #first is because `first` and `last` build a new array from the Range, we don't want that.. we just want the `begin` or `end` values instead.

Problem

Is there any way to push elements onto the end of a range without converting it into an array then back to a range? ``` r = 1..10 => 1..10 r << 11 NoMethodError: undefined method `<<' for 1..10:Range r.push 11 NoMethodError: undefined method `push' for 1..10:Range ```

Original source