Module: Cleaners::RemoveNonPrintable

Defined in:
lib/data_cleansing/cleaners.rb

Overview

Remove all not printable characters

Constant Summary collapse

NOT_PRINTABLE =
Regexp.compile(/[^[:print:]]/)

Class Method Summary collapse

Class Method Details

.call(string) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/data_cleansing/cleaners.rb', line 49

def self.call(string)
  return string unless string.is_a?(String)

  # Strip invalid characters, since they are non printable
  unless string.valid_encoding?
    string = string.encode(string.encoding, invalid: :replace, undef: :replace, replace: "")
  end
  string.gsub!(NOT_PRINTABLE, '') || string
end