class RuboCop::Cop::Performance::Sample
- 1, 2, 3].shuffle(random: Random.new)
[1, 2, 3].shuffle[foo, bar]
[1, 2, 3].shuffle[1..3 -
# sample(3) might return a longer Array
[1, 2, 3].shuffle[1, 3] # sample(3) might return a longer Array
[1, 2, 3].sample(3)
[1, 2, 3].sample
[1, 2, 3].shuffle
# good
[1, 2, 3].shuffle(random: Random.new).first
[1, 2, 3].shuffle # sample(3) will do the same
[1, 2, 3].shuffle[0, 2] # sample(2) will do the same
[1, 2, 3].shuffle[2]
[1, 2, 3].shuffle.last
[1, 2, 3].shuffle.first(2)
[1, 2, 3].shuffle.first
# bad
@example
and ‘shuffle[]` and change them to use `sample` instead.
This cop is used to identify usages of `shuffle.first`, `shuffle.last`
def autocorrect(node)
def autocorrect(node) ShuffleAnalyzer.new(node).autocorrect end
def on_send(node)
def on_send(node) analyzer = ShuffleAnalyzer.new(node) return unless analyzer.offensive? add_offense(node, analyzer.source_range, analyzer.message) end