Method: XDR::Concerns::ConvertsToXDR#to_xdr

Defined in:
lib/xdr/concerns/converts_to_xdr.rb

#to_xdr(val, encoding = 'raw') ⇒ String

Serialized the provided val to xdr, returning a string of the serialized data

Parameters:

  • val (Object)

    the value to serialize

Returns:

  • (String)

    the produced bytes



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 42

def to_xdr(val, encoding='raw')
  raw = StringIO.
    new.
    tap{|io| write(val, io)}.
    string.force_encoding("ASCII-8BIT")

  case encoding
  when 'raw' ; raw
  when 'base64' ; Base64.strict_encode64(raw)
  when 'hex' ; raw.unpack("H*").first
  else
    raise  ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
  end
end