Method: RubyExpect::Expect#initialize

Defined in:
lib/ruby_expect/expect.rb

#initialize(*args, &block) ⇒ Expect

Create a new Expect object for the given IO object

There are two ways to create a new Expect object. The first is to supply a single IO object with a read/write mode. The second method is to supply a read file handle as the first argument and a write file handle as the second argument.

args

at most 3 arguments, 1 or 2 IO objects (read/write or read + write and an optional options hash. The only currently supported option is :debug (default false) which, if enabled, will send data received on the input filehandle to STDOUT

block

An optional block called upon initialization. See procedure

Examples

# expect with a read/write filehandle
exp = Expect.new(rwfh)

# expect with separate read and write filehandles
exp = Expect.new(rfh, wfh)

# turning on debugging
exp = Expect.new(rfh, wfh, :debug => true)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_expect/expect.rb', line 76

def initialize *args, &block
  options = {}
  if (args.last.is_a?(Hash))
    options = args.pop
  end

  raise ArgumentError("First argument must be an IO object") unless (args[0].is_a?(IO))
  if (args.size == 1)
    @write_fh = args.shift
    @read_fh = @write_fh
  elsif (args.size == 2)
    raise ArgumentError("Second argument must be an IO object") unless (args[1].is_a?(IO))
    @write_fh = args.shift
    @read_fh = args.shift
  else
    raise ArgumentError.new("either specify a read/write IO object, or a read IO object and a write IO object")
  end
  
  raise "Input file handle is not readable!" unless (@read_fh.stat.readable?)
  raise "Output file handle is not writable!" unless (@write_fh.stat.writable?)

  @child_pid = options[:child_pid]
  @debug = options[:debug] || false
  @buffer = ''
  @before = ''
  @match = ''
  @timeout = 0

  unless (block.nil?)
    procedure(&block)
  end
end