Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/fsdb/read-write-object.rb

Overview

based on drb.rb

Instance Method Summary collapse

Instance Method Details

#read_objectObject

returns nil at eof

Raises:

  • (TypeError)


13
14
15
16
17
18
19
20
21
# File 'lib/fsdb/read-write-object.rb', line 13

def read_object
  sz = read(4)	# sizeof (N)
  return nil if sz.nil?
  raise TypeError, "incomplete header, size == #{sz.size}" if sz.size < 4
  sz = sz.unpack('N')[0]
  str = read(sz)
  raise TypeError, 'incomplete object' if str.nil? || str.size < sz
  Marshal::load(str)
end

#write_object(obj) ⇒ Object



6
7
8
9
10
# File 'lib/fsdb/read-write-object.rb', line 6

def write_object(obj)
  str = Marshal.dump(obj)
  packet = [str.size].pack('N') + str
  write(packet)
end