Class: Symbol

Inherits:
Object
  • Object
show all
Includes:
MessagePack::CoreExt
Defined in:
lib/msgpack/symbol.rb,
lib/msgpack/core_ext.rb

Class Method Summary collapse

Methods included from MessagePack::CoreExt

#to_msgpack

Class Method Details

.from_msgpack_ext(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/msgpack/symbol.rb', line 12

def self.from_msgpack_ext(data)
  # from_msgpack_ext is supposed to parse a binary string.
  # The canonical way to do it for symbols would be:
  #  data.unpack1('A*').to_sym
  # However in this instance we can take a shortcut

  # We assume the string encoding is UTF-8, and let Ruby create either
  # an ASCII symbol or UTF-8 symbol.
  data.force_encoding(Encoding::UTF_8).to_sym
rescue EncodingError
  # If somehow the string wasn't valid UTF-8 not valid ASCII, we fallback
  # to what has been the historical behavior of creating a binary symbol
  data.force_encoding(Encoding::BINARY).to_sym
end