Module: Unidecoder

Defined in:
lib/unidecode.rb

Constant Summary collapse

CODEPOINTS =

Contains Unicode codepoints, loading as needed from YAML files

Hash.new { |h, k|
  h[k] = YAML::load_file(File.join(File.dirname(__FILE__), "data", "#{k}.yml"))
}

Class Method Summary collapse

Class Method Details

.decode(string) ⇒ Object

Returns string with its UTF-8 characters transliterated to ASCII ones.

You're probably better off just using the added String#to_ascii.


13
14
15
16
17
18
19
20
21
# File 'lib/unidecode.rb', line 13

def decode(string)
  string.gsub(/[^\x00-\x7f]/u) do |codepoint|
    begin
      CODEPOINTS[code_group(codepoint)][grouped_point(codepoint)]
    rescue
      "?"
    end
  end
end