class String
Extensions for string class.
Constants
- WORD_RE
A regular expression for identifying words
Public Instance Methods
@return [String] a copy of the string converted into American spelling.
# File lib/anglicize/string.rb, line 19 def americanize # Load english to american hash if not already loaded @@english_to_american ||= load_english_to_american convert_all_words(@@english_to_american) end
Converts the string into American spelling. @return [String] the string converted into American spelling
# File lib/anglicize/string.rb, line 27 def americanize! replace(americanize) end
@return [String] a copy of the string converted into English spelling.
# File lib/anglicize/string.rb, line 6 def anglicize # Load american to english conversion hash if not already loaded @@american_to_english ||= load_american_to_english convert_all_words(@@american_to_english) end
Converts the string into English spelling. @return [String] the string converted into English spelling
# File lib/anglicize/string.rb, line 14 def anglicize! replace(anglicize) end
@param word [String] used as the case template @return [String] a copy of the string with the same case as the template.
# File lib/anglicize/string.rb, line 43 def copy_case(word) result = '' case word when word.upcase result = upcase when word.downcase result = downcase else chars.each_with_index do |char, index| word_char = word[index] || 'a' result << (word_char.upcase == word_char ? char.upcase : char.downcase) end end result end
Changes the string to have the same case as the string parameter. @param word [String] used as the case template @return [String] the string with the same case as the template.
# File lib/anglicize/string.rb, line 62 def copy_case!(_word) replace(copy_case) end
@return [Boolean] a value indicating whether the American spelling differs.
# File lib/anglicize/string.rb, line 37 def has_alternate_american_spelling? self != americanize end
@return [Boolean] a value indicating whether the English spelling differs.
# File lib/anglicize/string.rb, line 32 def has_alternate_english_spelling? self != anglicize end
Private Instance Methods
# File lib/anglicize/string.rb, line 71 def convert_all_words(conversion_hash) result = dup scan(WORD_RE).uniq.each do |word| replacement = get_word_from_conversion_hash(conversion_hash, word) result.gsub!(/\b#{word}\b/, replacement) end result end
# File lib/anglicize/string.rb, line 80 def get_word_from_conversion_hash(conversion_hash, word) key = word.downcase (conversion_hash[key] || word).copy_case(word) end
# File lib/anglicize/string.rb, line 96 def load_alternate_spellings dirname = File.dirname(File.expand_path(__FILE__)) file_path = File.join(dirname, 'alternate_spellings.yml') YAML.safe_load(File.open(file_path)) end
Load alternate spellings if not already loaded and insert values and keys
# File lib/anglicize/string.rb, line 86 def load_american_to_english @@alternate_spellings ||= load_alternate_spellings @@alternate_spellings.invert end
Load alternate spellings if not already loaded
# File lib/anglicize/string.rb, line 92 def load_english_to_american @@alternate_spellings ||= load_alternate_spellings end