Class: IO

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

Overview

Author

Nicolas Pouillard <[email protected]>.

Copyright

Copyright © 2005 Nicolas Pouillard. All rights reserved.

License

GNU General Public License (GPL).

Revision

$Id: /w/fey/ruby_ex/trunk/lib/commands/pipe.rb 21865 2006-02-18T17:13:28.680350Z pouillar $

Direct Known Subclasses

IOO

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pipeObject



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

def pipe
  ios = pipe_without_pipe?
  ios.each do |io|
    io.send(:define_singleton_method, :pipe?) { true }
  end
  ios
end

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

#pipe?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/commands/pipe.rb', line 8

def pipe?
  false
end

#pipe_without_pipe?Object



14
# File 'lib/commands/pipe.rb', line 14

alias_method :pipe_without_pipe?, :pipe