Module: RubyFish::Hamming

Defined in:
lib/rubyfish/hamming.rb

Class Method Summary collapse

Class Method Details

.distance(a, b, opts = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rubyfish/hamming.rb', line 2

def distance a, b, opts={}
  ignore_case = opts[:ignore_case]
  distance = 0
  as, bs = a.to_s, b.to_s
  
  if ignore_case
    as.downcase!
    bs.downcase!
  end

  short, long = [as, bs].sort

  long.chars.zip(short.chars).each {|ac, bc| distance += 1 if ac != bc }

  distance
end