Class: String

Inherits:
Object show all
Defined in:
lib/json_builder/xchar.rb,
lib/json_builder/jsonmarkup.rb

Overview

Enhance the String class with a XML escaped character version of to_s.

Instance Method Summary collapse

Instance Method Details

#lastObject



226
227
228
# File 'lib/json_builder/jsonmarkup.rb', line 226

def last
  self.split('')[self.length-1]
end

#to_xs(escape = true) ⇒ Object

XML escaped version of to_s. When escape is set to false the CP1252 fix is still applied but utf-8 characters are not converted to character entities.



100
101
102
103
104
# File 'lib/json_builder/xchar.rb', line 100

def to_xs(escape=true)
  unpack('U*').map {|n| n.xchr(escape)}.join # ASCII, UTF-8
rescue
  unpack('C*').map {|n| n.xchr}.join # ISO-8859-1, WIN-1252
end