Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- lib/fsdb/read-write-object.rb
Overview
based on drb.rb
Instance Method Summary collapse
-
#read_object ⇒ Object
returns nil at eof.
- #write_object(obj) ⇒ Object
Instance Method Details
#read_object ⇒ Object
returns nil at eof
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 |