Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hash.rb

Overview

This helper function will convert all hash values into UTF8

Instance Method Summary collapse

Instance Method Details

#to_utf8Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hash.rb', line 5

def to_utf8
  Hash[
    collect do |k, v|
      if v.respond_to?(:to_utf8)
        [k, v.to_utf8]
      elsif v.respond_to?(:encoding) && v.dup.respond_to?(:encode)
        [k, v.dup.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')]
      else
        [k, v]
      end
    end
  ]
end