Skip to content
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

Merged
merged 8 commits into from
Sep 20, 2023
Merged

Conversation

meatball133
Copy link
Member

No description provided.

Copy link
Member

@kotp kotp left a 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.

Comment on lines 8 to 11
```ruby
1..5 # => 1, 2, 3, 4, 5
1...5 # => 1, 2, 3, 4
```
Copy link
Member

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:

Suggested change
```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]

Comment on lines 15 to 19
Ranges can also be created using the `Range` initializer.

```ruby
Range.new(1, 5) # => 1, 2, 3, 4, 5
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

## 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines 53 to 57
| 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` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| 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` |


## Endless & Beginless ranges

There are two special types of ranges, the endless and beginless ranges.
Copy link
Member

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.


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.
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Looking in the docs are there also mention of nil: https://rubyapi.org/3.2/o/range.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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!"

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

```

```exercism/caution
If not used on a collection, the endless range can cause an infinite loop, if not used with caution.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Member Author

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.

## 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Its behavior can become when doing more complex string ranges, so use it with caution.

```ruby
"aa".."az" # => "aa", "ab", "ac", ..., "az"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"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.

concepts/ranges/about.md Outdated Show resolved Hide resolved
concepts/ranges/about.md Outdated Show resolved Hide resolved
@meatball133 meatball133 merged commit bd07012 into exercism:main Sep 20, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants