-
-
Notifications
You must be signed in to change notification settings - Fork 388
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
add spec for Array can be sliced with Enumerator::ArithmeticSequence #857
add spec for Array can be sliced with Enumerator::ArithmeticSequence #857
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.
Thanks, looks great!
@eregon You made me overdo it 😂 I was playing around with almost any combinations of
Then I learned that I ended up with many constellations, some of which broke my brain, particularly where steps are negative. For example (the last one only): # @array = [0, 1, 2, 3, 4, 5]
@array.send(@method, eval("(2..).step(-1)")).should == [2, 1, 0]
@array.send(@method, eval("(2..).step(-2)")).should == [2, 0]
@array.send(@method, eval("(2..).step(-10)")).should == [0] # ??? I would have expected [2] The same with entire blocks it "has beginless range and negative steps" {}
it "has inverted closed range and negative steps" {} Now, I have three questions for you:
Regarding the clean out: I think we can remove all assertions where the comment says:
because I don't think they add value to the specs, what do you think? |
@array.send(@method, eval("(2..).step(-10)")).should == [0] # ??? I would have expected [2] does seem weird, could you report it to bugs.ruby-lang.org/? |
😆 I was just asking to split in two, but more coverage is always welcome :) |
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.
because I don't think they add value to the specs, what do you think?
I think they still increase coverage, and they run fast so I would keep them.
For all the ???
, please report them and for now let's exclude those specs from this PR.
But please keep the current state on some other branch as they could be useful later on.
Maybe the behavior is actually intended for some of them although it's clearly very surprising to me too.
core/array/shared/slice.rb
Outdated
|
||
@array.send(@method, eval("(2..).step(-1)")).should == [2, 1, 0] | ||
@array.send(@method, eval("(2..).step(-2)")).should == [2, 0] | ||
@array.send(@method, eval("(2..).step(-10)")).should == [0] # ??? |
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 agree it's weird, please report it
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.
My version produced:
$ ./ruby --disable-gems -e "p [0, 1, 2, 3, 4, 5][(2..).step(-10)]"
[2]
(which seems reasonable, right?.. just the first one—at idx 2, then no more available items)
core/array/shared/slice.rb
Outdated
|
||
@array.send(@method, eval("(-3..).step(-1)")).should == [3, 2, 1, 0] | ||
@array.send(@method, eval("(-3..).step(-2)")).should == [3, 1] | ||
@array.send(@method, eval("(-3..).step(-10)")).should == [0] # ??? |
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 agree it's weird, please report it
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.
Mine was:
$ ./ruby --disable-gems -e "p [0, 1, 2, 3, 4, 5][(-3..).step(-10)]"
[3]
core/array/shared/slice.rb
Outdated
@array.send(@method, eval("(-3..).step(-10)")).should == [0] # ??? | ||
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.
I agree it's weird, please report it
core/array/shared/slice.rb
Outdated
# ???????? | ||
it "has inverted closed range and negative steps" do | ||
# start and end with positive index | ||
@array.send(@method, eval("(3..1).step(-1)")).should == [3, 2, 1] |
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.
This sounds reasonable
core/array/shared/slice.rb
Outdated
it "has inverted closed range and negative steps" do | ||
# start and end with positive index | ||
@array.send(@method, eval("(3..1).step(-1)")).should == [3, 2, 1] | ||
@array.send(@method, eval("(3...1).step(-1)")).should == [2, 1] |
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 agree it's weird, please report it. the 1 should be excluded, not the 3.
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.
Mine was
$ ./ruby --disable-gems -e "p [0, 1, 2, 3, 4, 5][(3...1).step(-1)]"
[3, 2]
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.
Left some comments. In general, I agree that most of what's marked with ???
is weird/should be reported as bugs :)
core/array/shared/slice.rb
Outdated
|
||
@array.send(@method, eval("(2..).step(-1)")).should == [2, 1, 0] | ||
@array.send(@method, eval("(2..).step(-2)")).should == [2, 0] | ||
@array.send(@method, eval("(2..).step(-10)")).should == [0] # ??? |
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.
My version produced:
$ ./ruby --disable-gems -e "p [0, 1, 2, 3, 4, 5][(2..).step(-10)]"
[2]
(which seems reasonable, right?.. just the first one—at idx 2, then no more available items)
core/array/shared/slice.rb
Outdated
|
||
@array.send(@method, eval("(-3..).step(-1)")).should == [3, 2, 1, 0] | ||
@array.send(@method, eval("(-3..).step(-2)")).should == [3, 1] | ||
@array.send(@method, eval("(-3..).step(-10)")).should == [0] # ??? |
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.
Mine was:
$ ./ruby --disable-gems -e "p [0, 1, 2, 3, 4, 5][(-3..).step(-10)]"
[3]
core/array/shared/slice.rb
Outdated
# ???????? | ||
it "has beginless range and negative steps" do | ||
# end with zero index | ||
@array.send(@method, eval("(..0).step(-1)")).should == [5, 4, 3, 2, 1, 0] |
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.
For this, my version gave the same answer. And I agree it doesn't look reasonable (going from the -infinity
to 0
, in steps of -1, you should never arrive here).
core/array/shared/slice.rb
Outdated
it "has inverted closed range and negative steps" do | ||
# start and end with positive index | ||
@array.send(@method, eval("(3..1).step(-1)")).should == [3, 2, 1] | ||
@array.send(@method, eval("(3...1).step(-1)")).should == [2, 1] |
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.
Mine was
$ ./ruby --disable-gems -e "p [0, 1, 2, 3, 4, 5][(3...1).step(-1)]"
[3, 2]
core/array/shared/slice.rb
Outdated
@array.send(@method, eval("(...-2).step(-2)")).should == [5] | ||
|
||
@array.send(@method, eval("(..-2).step(-10)")).should == [4] | ||
@array.send(@method, eval("(...-2).step(-10)")).should == [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.
OK, all of those should give just []
, it seems 🤷
core/array/shared/slice.rb
Outdated
@array.send(@method, eval("(-2...-4).step(-2)")).should == [3] | ||
|
||
@array.send(@method, eval("(-2..-4).step(-10)")).should == [2] | ||
@array.send(@method, eval("(-2...-4).step(-10)")).should == [2] |
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.
In general, if I understand/remember the reasoning correctly, with a mix of negative and positive indexes the logic is "convert all indexes to positive (e.g. -1 to ary.length - 1
), then iterate between them"
8b7fe61
to
d23057d
Compare
https://bugs.ruby-lang.org/issues/18247 I'll cleanup this branch, and copy the weird ones to a new branch on the weekend. 👍 |
d23057d
to
a3b33a8
Compare
Hi @eregon As you suggested, I moved the specs that were marked with Letme know if you want any more changes, thank you! |
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.
Thank you for the great work on these specs!
@eregon Thank you for your inputs and support! |
Hello 👋
Today I'll try to cover this from #823 :
Thank you for your feedback.