Module: Philiprehberger::FuzzyMatch::Hamming

Defined in:
lib/philiprehberger/fuzzy_match/hamming.rb

Overview

Hamming distance for equal-length strings

Counts the number of positions where corresponding characters differ. Both strings must have the same length.

Class Method Summary collapse

Class Method Details

.distance(str_a, str_b) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
# File 'lib/philiprehberger/fuzzy_match/hamming.rb', line 10

def self.distance(str_a, str_b)
  a = str_a.to_s.downcase
  b = str_b.to_s.downcase

  raise Error, "Strings must be the same length (got #{a.length} and #{b.length})" unless a.length == b.length

  a.chars.zip(b.chars).count { |c_a, c_b| c_a != c_b }
end