Method: Enco.to_utf8

Defined in:
lib/enco.rb

.to_utf8(string, options = {}) ⇒ Object

Parameters:

  • string (String)
  • options (Hash) (defaults to: {})


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/enco.rb', line 9

def self.to_utf8(string, options = {})
  return string unless string.is_a? String
  string = string.dup

  cd = ::CharDet.detect(string, :silent => true)

  if cd.confidence > 0.5 then
    string.encode('utf-8', cd.encoding, :invalid => :replace)
  else
    force_to_utf8 string
  end
end