Module: Edits::Compare
- Included in:
- Levenshtein, RestrictedEdit
- Defined in:
- lib/edits/compare.rb
Overview
Comparison helpers
Instance Method Summary collapse
-
#most_similar(prototype, strings) ⇒ String?
Given a prototype string and an array of strings, determines which string is most similar to the prototype.
Instance Method Details
#most_similar(prototype, strings) ⇒ String?
Given a prototype string and an array of strings, determines which string is most similar to the prototype.
most_similar("foo", strings) is functionally equivalent to
strings.min_by { |s| distance("foo", s) }, leveraging
distance_with_max.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/edits/compare.rb', line 19 def most_similar(prototype, strings) return nil if strings.empty? min_s = strings[0] min_d = distance(prototype, min_s) strings[1..].each do |s| return min_s if min_d.zero? d = distance_with_max(prototype, s, min_d) if d < min_d min_d = d min_s = s end end min_s end |