Class: String

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

Instance Method Summary collapse

Instance Method Details

#sanitize(encoding = "UTF-8") ⇒ Object



6
7
8
9
10
11
# File 'lib/tddium/util.rb', line 6

def sanitize(encoding="UTF-8")
  opts = {:invalid => :replace, :undef => :replace}
  d = self.dup
  d.force_encoding(encoding).valid_encoding? ?
    d : d.force_encoding("BINARY").encode(encoding, opts)
end

#sanitize!(encoding = "UTF-8") ⇒ Object



13
14
15
16
17
18
19
# File 'lib/tddium/util.rb', line 13

def sanitize!(encoding="UTF-8")
  opts = {:invalid => :replace, :undef => :replace}
  unless self.force_encoding(encoding).valid_encoding?
    self.force_encoding("BINARY")
    self.encode!(encoding, opts)
  end
end