---
title: "Ruby 2.6 adds endless range"
description: "Shorter syntax for endless range with index is added in Ruby 2.6"
canonical_url: "https://www.bigbinary.com/blog/ruby-2-6-adds-endless-range"
markdown_url: "https://www.bigbinary.com/blog/ruby-2-6-adds-endless-range.md"
---

# Ruby 2.6 adds endless range

Shorter syntax for endless range with index is added in Ruby 2.6

- Author: Taha Husain
- Published: July 4, 2018
- Categories: Ruby 2.6, Ruby

Before Ruby 2.6, if we want endless loop with index, we would need to use
[Float::INFINITY](http://ruby-doc.org/core-2.5.1/Float.html#INFINITY) with
[up to](http://ruby-doc.org/core-2.5.1/Integer.html#method-i-upto) or
[Range](http://ruby-doc.org/core-2.5.1/Range.html), or use
[Numeric#step](http://ruby-doc.org/core-2.5.1/Numeric.html#method-i-step).

##### Ruby 2.5.0

```ruby
irb> (1..Float::INFINITY).each do |n|
irb\* # logic goes here
irb> end
```

OR

```ruby
irb> 1.step.each do |n|
irb\* # logic goes here
irb> end
```

#### Ruby 2.6.0

Ruby 2.6 makes infinite loop more readable by changing mandatory second argument
in range to optional. Internally, Ruby changes second argument to `nil` if
second argument is not provided. So, both `(0..)` and `(0..nil)` are same in
Ruby 2.6.

###### Using endless loop in Ruby 2.6

```ruby
irb> (0..).each do |n|
irb\* # logic goes here
irb> end
```

```ruby
irb> (0..nil).size
=> Infinity
irb> (0..).size
=> Infinity
```

In Ruby 2.5, `nil` is not an acceptable argument and `(0..nil)` would throw
`ArgumentError`.

```ruby
irb> (0..nil)
ArgumentError (bad value for range)
```

Here is the relevant
[commit](https://github.com/ruby/ruby/commit/7f95eed19e22cb9a4867819355fe4ab99f85fd16)
and [discussion](https://bugs.ruby-lang.org/issues/12912) for this change.

The Chinese version of this blog is available
[here](http://madao.me/yi-ruby-2-6-zeng-jia-wu-qiong-fan-wei/).

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2-6-adds-endless-range)
