class CExpect::Reader

A class delegating normal operations to a wrapped IO, adding expect methods

Public Instance Methods

add_observer(*args) click to toggle source
# File lib/cexpect.rb, line 18
def add_observer(*args)
  extend Observable # overwrites this method with Observable#add_observer
  # Call the new add_observer method
  add_observer(*args)

  extend LoggingReader
end
expect(pat, timeout = nil, match_method: :re_match) { |result| ... } click to toggle source
# File lib/cexpect.rb, line 26
def expect(pat, timeout = nil, match_method: :re_match)
  buf = +''

  result = catch(:result) do
    loop { expect_try(pat, buf, timeout, match_method) }
  end

  if block_given?
    yield result
  else
    result
  end
end
fexpect(pat, timeout = nil) click to toggle source
# File lib/cexpect.rb, line 40
def fexpect(pat, timeout = nil)
  expect(pat, timeout, match_method: :string_match)
end

Private Instance Methods

expect_try(pat, buf, timeout, match_method) click to toggle source
# File lib/cexpect.rb, line 46
def expect_try(pat, buf, timeout, match_method)
  c = getc(timeout)

  throw(:result, nil) if c.nil?

  buf << c

  log(pat, buf) if respond_to?(:log)

  result = send(match_method, buf, pat)
  throw(:result, result) if result
end
getc(timeout) click to toggle source
# File lib/cexpect.rb, line 59
def getc(timeout)
  rd = __getobj__

  return nil if !IO.select([rd], nil, nil, timeout) || eof?

  rd.getc.chr
end
re_match(buf, pat) click to toggle source
# File lib/cexpect.rb, line 67
def re_match(buf, pat)
  buf.match(pat)
end
string_match(buf, pat) click to toggle source
# File lib/cexpect.rb, line 71
def string_match(buf, pat)
  buf[0, buf.size - pat.size] if buf.end_with?(pat)
end