Class: Cod::IOPair

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r = nil, w = nil) ⇒ IOPair

Returns a new instance of IOPair.



3
4
5
6
7
8
9
# File 'lib/cod/iopair.rb', line 3

def initialize(r=nil, w=nil)
  if r && w
    super(r, w)
  else
    super(*IO.pipe)
  end
end

Instance Attribute Details

#rObject

Returns the value of attribute r

Returns:

  • (Object)

    the current value of r



2
3
4
# File 'lib/cod/iopair.rb', line 2

def r
  @r
end

#wObject

Returns the value of attribute w

Returns:

  • (Object)

    the current value of w



2
3
4
# File 'lib/cod/iopair.rb', line 2

def w
  @w
end

Instance Method Details

#closeObject



27
28
29
30
# File 'lib/cod/iopair.rb', line 27

def close
  close_r
  close_w
end

#close_rObject



31
32
33
34
# File 'lib/cod/iopair.rb', line 31

def close_r
  r.close if r
  self.r = nil
end

#close_wObject



35
36
37
38
# File 'lib/cod/iopair.rb', line 35

def close_w
  w.close if w
  self.w = nil
end

#initialize_copy(other) ⇒ Object

Performs a deep copy of the structure.



12
13
14
15
16
# File 'lib/cod/iopair.rb', line 12

def initialize_copy(other)
  super
  self.r = other.r.dup if other.r
  self.w = other.w.dup if other.w
end

#read(serializer) ⇒ Object



22
23
24
25
26
# File 'lib/cod/iopair.rb', line 22

def read(serializer)
  close_w
  raise Cod::WriteOnlyChannel unless r
  serializer.de(r)
end

#write(buf) ⇒ Object



17
18
19
20
21
# File 'lib/cod/iopair.rb', line 17

def write(buf)
  close_r
  raise Cod::ReadOnlyChannel unless w
  w.write(buf)
end