123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::Duplication

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/duplication.rb

Overview

Common functionality for dealing with duplication.

Instance Method Summary

Instance Method Details

#consecutive_duplicates(collection) ⇒ Array (private)

Returns the consecutive duplicates, leaving out the first instance of the duplicated elements.

Parameters:

  • collection (Array)

    an array to return consecutive duplicates for

Returns:

  • (Array)

    the consecutive duplicates

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/duplication.rb', line 31

def consecutive_duplicates(collection)
  grouped_duplicates(collection).flat_map { |items| items[1..] }
end

#duplicates(collection) ⇒ Array (private)

Returns all duplicates, including the first instance of the duplicated elements.

Parameters:

  • collection (Array)

    an array to return duplicates for

Returns:

  • (Array)

    all the duplicates

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/duplication.rb', line 22

def duplicates(collection)
  grouped_duplicates(collection).flatten
end

#duplicates?(collection) ⇒ Boolean (private)

Whether the collection contains any duplicates.

Parameters:

  • collection (Array)

    an array to check for duplicates

Returns:

  • (Boolean)

    whether the array contains any duplicates

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/duplication.rb', line 13

def duplicates?(collection)
  collection.size > 1 && collection.size > collection.uniq.size
end

#grouped_duplicates(collection) ⇒ Array (private)

Returns a hash of grouped duplicates. The key will be the first instance of the element, and the value an array of the initial element and all duplicate instances.

Parameters:

  • collection (Array)

    an array to group duplicates for

Returns:

  • (Array)

    the grouped duplicates

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/duplication.rb', line 41

def grouped_duplicates(collection)
  collection.group_by { |item| item }.values.reject(&:one?)
end