Class: StringMatcher

Inherits:
Object show all
Defined in:
lib/sh_util.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str1, str2) ⇒ StringMatcher

Returns a new instance of StringMatcher.



53
54
55
# File 'lib/sh_util.rb', line 53

def initialize str1, str2
  @str1, @str2 = str1, str2
end

Class Method Details

.compare(str1, str2) ⇒ Object



57
58
59
# File 'lib/sh_util.rb', line 57

def self.compare(str1, str2)
  return StringMatcher.new(str1, str2).compare
end

.compare_ignore_case(str1, str2) ⇒ Object



61
62
63
# File 'lib/sh_util.rb', line 61

def self.compare_ignore_case(str1, str2)
  return StringMatcher.new(str1, str2).compare_ignore_case
end

Instance Method Details

#compareObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sh_util.rb', line 65

def compare
  return 1 if @str1 == @str2
  pairs1 = word_letter_pairs @str1
  pairs2 = word_letter_pairs @str2
  union = pairs1.size + pairs2.size
  intersection = 0
  pairs1.size.times do
    intersection += 1 if pairs2.delete pairs1.pop
  end
  return (2 * intersection).to_f / union
end

#compare_ignore_caseObject



77
78
79
80
81
# File 'lib/sh_util.rb', line 77

def compare_ignore_case
  @str1 = @str1.upcase
  @str2 = @str2.upcase
  return compare
end