Method: Ensure::Encoding.sniff_encoding

Defined in:
lib/ensure/encoding.rb

.sniff_encoding(string) ⇒ Object

Tries to guess the encoding of the string and returns the most likely encoding.



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

def self.sniff_encoding(string)
  first_bytes = string.unpack('C3')
  BYTE_ORDER_MARKS.each do |encoding, bytes|
    if first_bytes[0...bytes.length] == bytes
      return encoding
    end
  end
  ::Encoding::UTF_8
end