Class: LLM::Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/pipe.rb

Overview

The LLM::Pipe class wraps a pair of IO objects created by IO.pipe. It is used by llm.rb internals to manage process and stream communication through one small interface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binmode: false) ⇒ LLM::Pipe

Returns a new pipe.

Parameters:

  • binmode (Boolean) (defaults to: false)

    Whether both ends of the pipe should be switched to binary mode



24
25
26
27
# File 'lib/llm/pipe.rb', line 24

def initialize(binmode: false)
  @r, @w = IO.pipe
  [@r, @w].each(&:binmode) if binmode
end

Instance Attribute Details

#rIO (readonly)

Returns the reader

Returns:

  • (IO)

    Returns the reader



12
13
14
# File 'lib/llm/pipe.rb', line 12

def r
  @r
end

#wIO (readonly)

Returns the writer

Returns:

  • (IO)

    Returns the writer



17
18
19
# File 'lib/llm/pipe.rb', line 17

def w
  @w
end

Instance Method Details

#closevoid

This method returns an undefined value.

Closes both ends of the pipe.



62
63
64
65
# File 'lib/llm/pipe.rb', line 62

def close
  [@r, @w].each(&:close)
rescue IOError
end

#close_readervoid

This method returns an undefined value.

Closes the reader.



70
71
72
73
# File 'lib/llm/pipe.rb', line 70

def close_reader
  @r.close
rescue IOError
end

#close_writervoid

This method returns an undefined value.

Closes the writer.



78
79
80
81
# File 'lib/llm/pipe.rb', line 78

def close_writer
  @w.close
rescue IOError
end

#closed?Boolean

Returns true when both ends are closed.

Returns:

  • (Boolean)


55
56
57
# File 'lib/llm/pipe.rb', line 55

def closed?
  [@r, @w].all?(&:closed?)
end

#flushvoid

This method returns an undefined value.

Flushes the writer.



48
49
50
# File 'lib/llm/pipe.rb', line 48

def flush
  @w.flush
end

#read_nonblockString

Reads from the reader end without blocking.

Returns:

  • (String)

Raises:

  • (IO::EAGAINWaitReadable)

    When no data is available to read



34
35
36
# File 'lib/llm/pipe.rb', line 34

def read_nonblock(...)
  @r.read_nonblock(...)
end

#writeInteger

Writes to the writer.

Returns:

  • (Integer)


41
42
43
# File 'lib/llm/pipe.rb', line 41

def write(...)
  @w.write(...)
end