-
-
Notifications
You must be signed in to change notification settings - Fork 518
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
[New Concept]: Range #1569
[New Concept]: Range #1569
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is about as far as I had time to go through, but please see the suggested changes, and there are some statements that I believe are not correct that should be changed as well.
concepts/ranges/about.md
Outdated
```ruby | ||
1..5 # => 1, 2, 3, 4, 5 | ||
1...5 # => 1, 2, 3, 4 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return is not what this statement returns, as we can see. Using IRB, since IRB "evaluated" syntax is being used here in the code example:
```ruby | |
1..5 # => 1, 2, 3, 4, 5 | |
1...5 # => 1, 2, 3, 4 | |
``` | |
In IRB: | |
```ruby | |
>> (1..5) | |
=> 1..5 | |
>> (1...5) | |
=> 1...5 | |
``` |
What do you think? The return is not like it would be with an Array.
Probably not in this section, but we can coerce this to an Array to show what is originally here:
>> (1..5).to_a
=> [1, 2, 3, 4, 5]
>> (1...5).to_a
=> [1, 2, 3, 4]
concepts/ranges/about.md
Outdated
Ranges can also be created using the `Range` initializer. | ||
|
||
```ruby | ||
Range.new(1, 5) # => 1, 2, 3, 4, 5 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ranges can also be created using the `Range` initializer. | |
```ruby | |
Range.new(1, 5) # => 1, 2, 3, 4, 5 | |
``` | |
Ranges can also be created using the `Range` initializer. | |
Exploring in IRB: | |
```ruby | |
>> Range.new(1, 5) | |
=> 1..5 |
concepts/ranges/about.md
Outdated
## Getting substrings | ||
|
||
When wanting to slice a string, you can use the range operator to get a substring. | ||
That is by creating a range with the start and end index of the substring. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is by creating a range with the start and end index of the substring. | |
That is, by creating a range with the start and end index of the sub-string. |
concepts/ranges/about.md
Outdated
| Method | Description | Example | | ||
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------- | | ||
| [`sum`][sum] | Returns the sum of all the values in the range | `(1..5).sum # => 15` | | ||
| [`size`][size] | Returns the size of the range | `(1..5).size # => 5` | | ||
| [`include?`][indlude] | Returns `true` if the range includes the given value, otherwise `false` | `(1..5).include?(3) # => true` | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Method | Description | Example | | |
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------- | | |
| [`sum`][sum] | Returns the sum of all the values in the range | `(1..5).sum # => 15` | | |
| [`size`][size] | Returns the size of the range | `(1..5).size # => 5` | | |
| [`include?`][indlude] | Returns `true` if the range includes the given value, otherwise `false` | `(1..5).include?(3) # => true` | | |
| Method | Description | Example | | |
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------- | | |
| [`sum`][sum] | Returns the sum of all the values in the range | `(1..5).sum # => 15` | | |
| [`size`][size] | Returns the size of the range | `(1..5).size # => 5` | | |
| [`include?`][indlude] | Returns `true` if the range includes the given value, otherwise `false` | `(1..5).include?(3) # => true` | |
concepts/ranges/about.md
Outdated
|
||
## Endless & Beginless ranges | ||
|
||
There are two special types of ranges, the endless and beginless ranges. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if they are really special ranges, or if this is more "syntactic sugar" that lets us express the normal ranges like this.
concepts/ranges/about.md
Outdated
|
||
There are two special types of ranges, the endless and beginless ranges. | ||
This means that the range has no beginning or end. | ||
The endless or beginless range has there start or end value being `nil`, but when defining the range so can nil be omitted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
irb(main):004:0> Range.new(nil, 5)
ArgumentError: bad value for range
from (irb):4:in `initialize'
from (irb):4:in `new'
from (irb):4
from :0
irb(main):005:0> (nil..5)
ArgumentError: bad value for range
from (irb):5
from :0
irb(main):006:0>
I do not think this is true, demonstrably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking in the docs are there also mention of nil: https://rubyapi.org/3.2/o/range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. That is why I said think... and did it in an REPL, to verify. Seems like it has changed for newer versions of Ruby.
So I would then expect it to no longer be a terminated by infinity anything, and perhaps "undefined" in behavior. I will definitely look at the source code. Pretty sure the use will not change from 1..infinity
as 1..nil
though. (That would be surprising, but nil
evaluates as 0 if we do to_i
and as 0.0
if we do nil.to_f
so definitely worth investigating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you sent a picture too. Whatever it is, I a sure it supports your statement. Not at a good device at the moment to get interpretations for pictures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for information, I happened to have a Pry session open, where Pry has a bug that does not show this as it is supposed to. That was the reason it was broken as shown. Not the syntax itself. "Bad Pry!"
concepts/ranges/about.md
Outdated
This means that the range has no beginning or end. | ||
The endless or beginless range has there start or end value being `nil`, but when defining the range so can nil be omitted. | ||
|
||
Using beginless and endless ranges is useful when you want to for example slice a string from the beginning or to the end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using beginless and endless ranges is useful when you want to for example slice a string from the beginning or to the end. | |
Using beginless and endless ranges is useful when you want to, for example, slice a string from the beginning or to the end. |
concepts/ranges/about.md
Outdated
``` | ||
|
||
```exercism/caution | ||
If not used on a collection, the endless range can cause an infinite loop, if not used with caution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If not used on a collection, the endless range can cause an infinite loop, if not used with caution. | |
If not used on a collection, the endless range can cause an endless sequence, if not used with caution. |
Because it does not loop, it is more an endless sequence than and endless loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I mean it could cause an infinte loop if you iterate over the sequence, but yes I think saying it is an endless sequence is better.
concepts/ranges/about.md
Outdated
## String ranges | ||
|
||
Strings can also be used in ranges and allow one to get an interval of strings between two strings. | ||
But its behavior is a bit different than with chars, when using multiple characters in a string range. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But its behavior is a bit different than with chars, when using multiple characters in a string range. | |
But its behavior is a bit different than with `chars`, when using multiple characters in a string range. |
I believe you may be referring to the method, and so that should be code formatted.
concepts/ranges/about.md
Outdated
Its behavior can become when doing more complex string ranges, so use it with caution. | ||
|
||
```ruby | ||
"aa".."az" # => "aa", "ab", "ac", ..., "az" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"aa".."az" # => "aa", "ab", "ac", ..., "az" | |
In IRB: | |
>> "aa".."az" | |
=> "aa", "ab", "ac", # continues until "az" |
We could use a smaller sequence as well, and still demonstrate this, so we do not have to allude to the missing content.
Co-authored-by: Victor Goff <[email protected]>
No description provided.