Skip to content

Commit 39d176b

Browse files
WizardOfOgzbbatsov
authored andcommitted
Change Array check splat(*) example from "good" to "bad": (rubocop#541)
Using the splat technique `[*object]` will _always_ instantiate a new Array object, whereas `Array(object)` will return the same object it is given as an argument if that argument is already an Array.
1 parent bd6f8a2 commit 39d176b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1755,18 +1755,18 @@ no parameters.
17551755
# => 'one, two, three'
17561756
```
17571757

1758-
* <a name="splat-arrays"></a>
1759-
Use `[*var]` or `Array()` instead of explicit `Array` check, when dealing
1758+
* <a name="array-coercion"></a>
1759+
Use `Array()` instead of explicit `Array` check or `[*var]`, when dealing
17601760
with a variable you want to treat as an Array, but you're not certain it's an
17611761
array.
1762-
<sup>[[link](#splat-arrays)]</sup>
1762+
<sup>[[link](#array-coercion)]</sup>
17631763

17641764
```Ruby
17651765
# bad
17661766
paths = [paths] unless paths.is_a? Array
17671767
paths.each { |path| do_something(path) }
17681768

1769-
# good
1769+
# bad (always creates a new Array instance)
17701770
[*paths].each { |path| do_something(path) }
17711771

17721772
# good (and a bit more readable)

0 commit comments

Comments
 (0)