Class: Treat::Helpers::Help
- Inherits:
-
Object
- Object
- Treat::Helpers::Help
- Defined in:
- lib/treat/helpers/help.rb
Overview
Helper methods to detect misspellings and suggest alternatives to the user.
Class Method Summary collapse
-
.did_you_mean?(list, name) ⇒ Boolean
Search the list to see if there are words similar to #name in the #list If yes, return a string saying “Did you mean …
Class Method Details
.did_you_mean?(list, name) ⇒ Boolean
Search the list to see if there are words similar to #name in the #list If yes, return a string saying “Did you mean … ?” with the names.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/treat/helpers/help.rb', line 9 def self.did_you_mean?(list, name) return '' # Fix list = list.map { |e| e.to_s } name = name.to_s sugg = [] list.each do |element| l = self.levenshtein(element,name) if l > 0 && l < 2 sugg << element end end unless sugg.size == 0 if sugg.size == 1 msg += " Perhaps you meant '#{sugg[0]}' ?" else sugg_quote = sugg[0..-2].map do |x| '\'' + x + '\'' end msg += " Perhaps you meant " + "#{sugg_quote.join(', ')}," + " or '#{sugg[-1]}' ?" end end msg end |