Class: Erlectricity::Port
- Inherits:
-
Object
- Object
- Erlectricity::Port
- Defined in:
- lib/erlectricity/port.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#skipped ⇒ Object
readonly
Returns the value of attribute skipped.
Instance Method Summary collapse
-
#initialize(input = STDIN, output = STDOUT) ⇒ Port
constructor
A new instance of Port.
- #receive ⇒ Object
- #restore_skipped ⇒ Object
- #send(term) ⇒ Object
Constructor Details
#initialize(input = STDIN, output = STDOUT) ⇒ Port
Returns a new instance of Port.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/erlectricity/port.rb', line 7 def initialize(input=STDIN, output=STDOUT) @input = input @output = output input.sync = true output.sync = true @encoder = Erlectricity::Encoder.new(nil) @skipped = [] @queue = [] end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
3 4 5 |
# File 'lib/erlectricity/port.rb', line 3 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
3 4 5 |
# File 'lib/erlectricity/port.rb', line 3 def output @output end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
5 6 7 |
# File 'lib/erlectricity/port.rb', line 5 def queue @queue end |
#skipped ⇒ Object (readonly)
Returns the value of attribute skipped.
4 5 6 |
# File 'lib/erlectricity/port.rb', line 4 def skipped @skipped end |
Instance Method Details
#receive ⇒ Object
19 20 21 |
# File 'lib/erlectricity/port.rb', line 19 def receive queue.empty? ? read_from_input : queue.shift end |
#restore_skipped ⇒ Object
31 32 33 |
# File 'lib/erlectricity/port.rb', line 31 def restore_skipped @queue = self.skipped + self.queue end |
#send(term) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/erlectricity/port.rb', line 23 def send(term) @encoder.out = StringIO.new('', 'w') @encoder.write_any(term) data = @encoder.out.string output.write([data.length].pack("N")) output.write(data) end |