File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -4552,6 +4552,48 @@ obj.then { |x| x.do_something }
45524552
45534553NOTE: You can read more about the rationale behind this guideline https://bugs.ruby-lang.org/issues/14594[here].
45544554
4555+ === Slicing with Ranges
4556+
4557+ `[0..-1]` in `ary[0..-1]` is redundant and simply synonymous with `ary`.
4558+
4559+ [source,ruby]
4560+ ----
4561+ # bad
4562+ ary[0..-1]
4563+ ary[0..nil]
4564+ ary[0...nil]
4565+
4566+ # good
4567+ ary
4568+ ----
4569+
4570+ Ruby 2.6 introduced endless ranges.
4571+
4572+ [source,ruby]
4573+ ----
4574+ # bad
4575+ ary[1..-1]
4576+ ary[1..nil]
4577+
4578+ # good
4579+ ary[1..]
4580+ ----
4581+
4582+ Ruby 2.7 introduced beginless ranges. But, unlike the somewhat obscure `-1` in `ary[1..-1]`, the `0` in `ary[0..42]` is clear
4583+ as a starting point. In fact, changing it to `ary[..42]` could potentially make it less readable. Therefore, `ary[0..42]`
4584+ should respect the original programmer's intent. On the other hand, `ary[nil..42]` could be replaced with `ary[..42]`.
4585+ Similarly, `ary[1..nil]` could be replaced with `ary[1..]`.
4586+
4587+ [source,ruby]
4588+ ----
4589+ # bad
4590+ ary[nil..42]
4591+
4592+ # good
4593+ ary[..42]
4594+ ary[0..42]
4595+ ----
4596+
45554597== Numbers
45564598
45574599=== Underscores in Numerics [[underscores-in-numerics]]
You can’t perform that action at this time.
0 commit comments