-
-
Notifications
You must be signed in to change notification settings - Fork 255
Improve description of range literal #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RX14
merged 3 commits into
crystal-lang:master
from
straight-shoota:feature/document-beginless-endless-ranges
Apr 14, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,32 @@ | ||
| # Range | ||
|
|
||
| A [Range](http://crystal-lang.org/api/Range.html) is typically constructed with a range literal: | ||
| A [Range](http://crystal-lang.org/api/Range.html) represents an interval between two values. It is typically constructed with a range literal, consisting of two or three dots: | ||
|
|
||
| ```crystal | ||
| x..y # an inclusive range, in mathematics: [x, y] | ||
| x...y # an exclusive range, in mathematics: [x, y) | ||
| * `x..y`: Two dots denote an inclusive range, including `x` and `y` and all values in between (in mathematics: `[x, y]`) . | ||
| * `x...y`: Three dots denote an exclusive range, including `x` and all values up to but not including `y` (in mathematics: `[x, y)`). | ||
|
straight-shoota marked this conversation as resolved.
|
||
|
|
||
| # Example: | ||
| ```cr | ||
| (0..5).to_a # => [0, 1, 2, 3, 4, 5] | ||
| (0...5).to_a # => [0, 1, 2, 3, 4] | ||
| ``` | ||
|
|
||
| **NOTE:** Range literals are often wrapped in parentheses, for example if it is meant to be used as the receiver of a call. `0..5.to_a` without parentheses would be semantically equivalent to `0..(5.to_a)` because method calls and other operators have higher precedence than the range literal. | ||
|
|
||
| An easy way to remember which one is inclusive and which one is exclusive it to think of the extra dot as if it pushes *y* further away, thus leaving it outside of the range. | ||
|
|
||
| The literal `x..y` is semantically equivalent to the explicit constructor `Range.new(x, y)` and `x...y` to `Range.new(x, y, true)`. | ||
|
|
||
| The begin and end values do not necessarily need to be of the same type: `true..1` is a valid range, although pretty useless `Enumerable` methods won't work with incompatible types. They need at least to be comparable. | ||
|
|
||
| Ranges with `nil` as begin are called begin-less and `nil` as end are called end-less ranges. In the literal notation, `nil` can be omitted: `x..` is an end-less range starting from `x`, and `..x` is an begin-less range ending at `x`. | ||
|
|
||
| ```cr | ||
| numbers = [1, 10, 3, 4, 5, 8] | ||
| numbers.select(6..) # => [10, 8] | ||
| numbers.select(..6) # => [1, 3, 4, 5] | ||
|
|
||
| numbers[2..] = [3, 4, 5, 8] | ||
| numbers[..2] = [1, 10, 3] | ||
| ``` | ||
|
|
||
| A range that is both begin-less and end-less is valid and can be expressed as `..` or `...` but it's typically not very useful. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.