class String

Extensions for string class.

Constants

WORD_RE

A regular expression for identifying words

Public Instance Methods

americanize() click to toggle source

@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
americanize!() click to toggle source

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
anglicize() click to toggle source

@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
anglicize!() click to toggle source

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
copy_case(word) click to toggle source

@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
copy_case!(_word) click to toggle source

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
has_alternate_american_spelling?() click to toggle source

@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
has_alternate_english_spelling?() click to toggle source

@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

convert_all_words(conversion_hash) click to toggle source
# 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
get_word_from_conversion_hash(conversion_hash, word) click to toggle source
# 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
load_alternate_spellings() click to toggle source
# 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_american_to_english() click to toggle source

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_english_to_american() click to toggle source

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