Class: Invoca::Utils::GuaranteedUTF8String

Inherits:
Object
  • Object
show all
Defined in:
lib/invoca/utils/guaranteed_utf8_string.rb

Constant Summary collapse

REPLACE_CHARACTER =

chosen because this is a 1-byte ASCII character that is not used in any of the popular escaping systems: XML, HTML, HTTP URIs, HTTP Form Post, JSON

'~'

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ GuaranteedUTF8String

Returns a new instance of GuaranteedUTF8String.



7
8
9
10
11
12
13
14
15
# File 'lib/invoca/utils/guaranteed_utf8_string.rb', line 7

def initialize(string)
  if string.is_a?(String) ||
    (string.respond_to?(:to_s) &&
     string.method(:to_s).owner != Kernel)  # the lame .to_s from Kernel just calls .inspect :(
    @string = string.to_s
  else
    raise ArgumentError, "#{self.class} must be initialized with a string or an object with a non-Kernel .to_s method but instead was #{string.class} #{string.inspect}"
  end
end

Instance Method Details

#to_stringObject Also known as: to_s



17
18
19
# File 'lib/invoca/utils/guaranteed_utf8_string.rb', line 17

def to_string
  @to_string ||= normalize_string(@string)
end