liveBook · Manning

Range

As explained in The Well-Grounded Rubyist, Fourth Edition

Range is Ruby's built-in object representing a sequence bounded by beginning and ending values, or by a beginning value alone for an endless range.

A Ruby range marks off values between endpoints. Its first job is answering whether a value falls inside it; when its values can be stepped through, it can also act as a collection. An integer range can be enumerated, but a range starting at one float and ending at another cannot. section 9.4

Choose the endpoint

`1..100` includes 100; `1...100` excludes it. `Range.new(1, 100)` is inclusive by default, while `Range.new(1, 100, true)` excludes the endpoint. An endless range such as `(1000...)` has no endpoint to include or exclude. Beginless ranges can still make that distinction: `(..1000)` includes 1000, and `(...1000)` does not. section 9.4.1

Use `begin` and `end` to inspect a range’s endpoints, and `exclude_end?` to check whether the ending value counts. Even `1...10` reports 10 as its `end`: exclusion changes the inclusion test, not the stored endpoint. section 9.4.2

Covered is not always included

For `r = "a".."z"`, `r.cover?("abc")` is true because `"abc"` compares between the endpoints. `r.include?("abc")` is false: the string range treats its members as the successive values from `"a"` through `"z"`. `member?` is another name for `include?`. Where a range cannot be treated as a finite collection, as with `1.0..2.0`, `include?` instead uses numerical comparison. section 9.4.2

Ranges also work as array and string slice positions: `['a','b','c','d'][0...-2]` returns `["a", "b"]`. For collection queries, `one?` and `none?` work on an integer range, but attempting `one?` on a float-starting range raises `TypeError`. A range can also serve as the criterion for `Enumerable#grep`: `[75, "hello", 10...20, "goodbye"].grep(50..100)` returns `[75]`. section 9.4.2 section 10.2 section 10.3.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through construction, endpoint inspection, contrasting inclusion tests, and enumeration limits; leaves fuller enumeration treatment to Enumerable.
Examples
Contrasts string-range membership tests, integer and float enumeration, collection slices, and range-based grep results.
What it assumes
Assumes familiarity with Ruby method calls and blocks while introducing range literals and Range.new through short examples.
What you can do after
Apply it via Ruby range literals and inclusion methods to test values, slice collections, and select with Enumerable#grep.
How it connects
Connects ranges to Enumerable, array and string slicing, and case equality.

Also explained in

Compare how 8 books explain range →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.