Style guide: misleading description for "Slicing with Ranges"
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 48/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- ruby
- Domain
- documentation
Research direction
Start with the linked Style guide section, “Slicing with Ranges,” and verify the distinction between ary[0..-1] and ary using the examples in the issue. Update the guidance to distinguish copying from aliasing and state whether ary.clone, ary.dup, [*ary], or simply ary is preferred, including the noted singleton-class caveat if clone is recommended.
Written by the indexing model from the issue text.
Description
In the style guide under Slicing with Ranges it's said that
[0..-1]inary[0..-1]is redundant and simply synonymous withary.
However, one makes a shallow copy while the other doesn't:
ary = [1, 2, 3]
with_slice = ary[0..-1]
without_slice = ary
ary[0] = :changed
# [1, 2, 3]
puts with_slice.inspect
# [:changed, 2, 3]
puts without_slice.inspect
By analogy to the Python idiom ary[:], I would guess that the "bad" form is often written specifically to make a copy rather than an alias.
I suggest that the advice be changed to say something like ary.clone, ary.dup or [*ary] is preferred (whatever is considered better style), or that simply ary is preferred if a copy isn't needed. If clone is a good alternative then it's worth adding that it isn't exactly synonymous with a full slice, since even ary.clone(freeze: false) clones the singleton class, whereas slicing doesn't.
- Dominant language
- No language data
- Stars
- 16.5k
- Forks
- 3.3k
- PR merge metrics
- No merged PRs in 30d
Getting set up
We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rubocop/ruby-style-guide
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
rubocop/ruby-style-guide#982 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
rubocop/ruby-style-guide#884 · 3 reactions ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
rubocop/ruby-style-guide#968 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
rubocop/ruby-style-guide#961 · 2 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
rubocop/ruby-style-guide#957 ·
All issues in rubocop/ruby-style-guide
Similar issues
-
sync-en
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
external
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
langchain-ai/docs#6255 ·
Maintainers usually reply within 1 day
-
detectors enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
SM260845/readme-gen#1 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
angular/angularfire#3774 ·
Maintainers usually reply within 2 days
-
good first issue help wanted opensource september
Difficulty 1/5 Under an hour Newbie friendliness 88/100