Module: RuboCop::NameSimilarity Private

Defined in:
lib/rubocop/name_similarity.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Common functionality for finding names that are similar to a given name.

Class Method Summary collapse

Class Method Details

.find_similar_name(target_name, names) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
12
13
# File 'lib/rubocop/name_similarity.rb', line 9

def find_similar_name(target_name, names)
  similar_names = find_similar_names(target_name, names)

  similar_names.first
end

.find_similar_names(target_name, names) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubocop/name_similarity.rb', line 15

def find_similar_names(target_name, names)
  # DidYouMean::SpellChecker is not available in all versions of Ruby, and
  # even on versions where it *is* available (>= 2.3), it is not always
  # required correctly. So we do a feature check first.
  # See: https://github.com/rubocop/rubocop/issues/7979
  return [] unless defined?(DidYouMean::SpellChecker)

  names = names.dup
  names.delete(target_name)

  spell_checker = DidYouMean::SpellChecker.new(dictionary: names)
  spell_checker.correct(target_name)
end