Class: Cod::SimpleSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/cod/simple_serializer.rb

Overview

The simplest of all serializers, one that uses Marshal.dump and Marshal.load as a message format. Use this as a template for your own wire format serializers.

Instance Method Summary collapse

Instance Method Details

#de(io) ⇒ Object

Reads as many bytes as needed from io to reconstruct one message. Turns the message back into a Ruby object according to the rules of the serializer. In this implementation, it will use Marshal.load to turn the object from a String to a Ruby Object.

Parameters:

  • io (IO)

    to read one message from

Returns:

  • (Object)

    that has been deserialized



26
27
28
29
30
31
32
# File 'lib/cod/simple_serializer.rb', line 26

def de(io)
  if block_given?
    Marshal.load(io, Proc.new)
  else
    Marshal.load(io)
  end
end

#en(obj) ⇒ String

Serializes obj into a format that can be transmitted via the wire. In this implementation, it will use Marshal.dump to turn the obj into a string.

Parameters:

  • obj (Object)

    to dump

Returns:

  • (String)

    transmitted over the wire



14
15
16
# File 'lib/cod/simple_serializer.rb', line 14

def en(obj)
  Marshal.dump(obj)
end