Class: IO

Inherits:
Object show all
Defined in:
lib/io_marshal.rb

Overview

module IoMarshal

Direct Known Subclasses

IOO

Instance Method Summary collapse

Instance Method Details

#dump(*objs) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/io_marshal.rb', line 15

def dump(*objs)
  objs.each do |obj|
    data = Marshal.dump(obj)
    write(data.size.to_s)
    write("\0")
    write(data)
  end
end

#load(num_objs = nil, non_block = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/io_marshal.rb', line 24

def load(num_objs=nil, non_block=false)
  objs = []
  n = num_objs
  begin
    return nil if eof? and non_block
    objs << load_one_object(non_block)
    unless n.nil?
      n -= 1
      break if n <= 0
    end
  end until eof?
  objs
end

#load_one_object(non_block = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/io_marshal.rb', line 38

def load_one_object(non_block=false)
  size = ''
  while (c = read(1)) != "\0" do
    if c.nil?
      if non_block
        return nil
      else
        sleep(0.1)
        redo
      end
    end
    size += c
  end
  size = size.to_i
  data = read(size)
  Marshal.load(data)
end