Module: Unaccent

Defined in:
lib/unaccent.rb,
lib/unaccent/string.rb,
lib/unaccent/version.rb

Defined Under Namespace

Modules: String

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Class Method Summary collapse

Class Method Details

.unaccent(str) ⇒ String

Replace a string’s accented characters with unaccented characters.

Examples:

str = 'Å Ç ß'
Unaccent.unaccent(str) = > 'AA C ss'

Returns:

  • (String)

    a string that has no accents



14
15
16
# File 'lib/unaccent.rb', line 14

def unaccent(str)
  via_gsub(str)
end

.via_each_char(str) ⇒ String

Replace a string’s accented characters with unaccented characters, by using string ‘#each_char` to iterate on characters.

Examples:

str = 'Å Ç ß'
Unaccent.via_each_char(str) = > 'AA C ss'

Returns:

  • (String)

    a string that has no accents



57
58
59
60
61
# File 'lib/unaccent.rb', line 57

def via_each_char(str)
  return str if str.ascii_only?

  res = ''; str.each_char { |c| res << ACCENTMAP.fetch(c, c) }; res
end

.via_gsub(str) ⇒ String

Replace a string’s accented characters with unaccented characters, by using string ‘#gsub` to replace non-ascii characters.

Examples:

str = 'Å Ç ß'
Unaccent.via_gsub(str) = > 'AA C ss'

Returns:

  • (String)

    a string that has no accents



27
28
29
30
31
# File 'lib/unaccent.rb', line 27

def via_gsub(str)
  return str if str.ascii_only?

  str.gsub(/[^[:ascii:]]/) { |c| ACCENTMAP.fetch(c, c) }
end

.via_scan(str) ⇒ String

Replace a string’s accented characters with unaccented characters, by using string ‘#scan` to iterate on characters.

Examples:

str = 'Å Ç ß'
Unaccent.via_scan(str) = > 'AA C ss'

Returns:

  • (String)

    a string that has no accents



42
43
44
45
46
# File 'lib/unaccent.rb', line 42

def via_scan(str)
  return str if str.ascii_only?

  res = ''; str.scan(/./) { |c| res << ACCENTMAP.fetch(c, c) }; res
end

.via_split_map(str) ⇒ String

Replace a string’s accented characters with unaccented characters, by using string ‘#split` and `#map` to iterate on characters.

Examples:

str = 'Å Ç ß'
Unaccent.via_split_map(str) = > 'AA C ss'

Returns:

  • (String)

    a string that has no accents



72
73
74
75
76
# File 'lib/unaccent.rb', line 72

def via_split_map(str)
  return str if str.ascii_only?

  str.split(//u).map { |c| ACCENTMAP.fetch(c, c) }.join
end