def expect *patterns, &block
      @logger.debug("Expecting: #{patterns.inspect}") if @logger.debug?
      patterns = pattern_escape(*patterns)
      @end_time = 0
      if (@timeout != 0)
        @end_time = Time.now + @timeout
      end

      @before = ''
      matched_index = nil
      while (@end_time == 0 || Time.now < @end_time)
        raise ClosedError.new("Read filehandle is closed") if (@read_fh.closed?)
        break unless (read_proc)
        @last_match = nil
        patterns.each_index do |i|
          if (match = patterns[i].match(@buffer))
            log_buffer(true)
            @logger.debug("  Matched: #{match}") if @logger.debug?
            @last_match = match
            @before = @buffer.slice!(0...match.begin(0))
            @match = @buffer.slice!(0...match.to_s.length)
            matched_index = i
            break
          end
        end
        unless (@last_match.nil?)
          unless (block.nil?)
            instance_eval(&block)
          end
          return matched_index
        end
      end
      @logger.debug("Timeout")
      return nil
    end
def expect *patterns, &block
      @logger.debug("Expecting: #{patterns.inspect}") if @logger.debug?
      patterns = pattern_escape(*patterns)
      @end_time = 0
      if (@timeout != 0)
        @end_time = Time.now + @timeout
      end

      @before = ''
      matched_index = nil
      while (@end_time == 0 || Time.now < @end_time)
        raise ClosedError.new("Read filehandle is closed") if (@read_fh.closed?)
        break unless (read_proc)
        @last_match = nil
        patterns.each_index do |i|
          if (match = patterns[i].match(@buffer))
            log_buffer(true)
            @logger.debug("  Matched: #{match}") if @logger.debug?
            @last_match = match
            @before = @buffer.slice!(0...match.begin(0))
            @match = @buffer.slice!(0...match.to_s.length)
            matched_index = i
            break
          end
        end
        unless (@last_match.nil?)
          unless (block.nil?)
            instance_eval(&block)
          end
          return matched_index
        end
      end
      @logger.debug("Timeout")
      return nil
    end
Wait until either the timeout occurs or one of the given patterns is seen
 in the input.  Upon a match, the property before is assigned all input in
 the accumulator before the match, the matched string itself is assigned to
 the match property and an optional block is called

 The method will return the index of the matched pattern or nil if no match
 has occurred during the timeout period

 +patterns+::
   list of patterns to look for.  These can be either literal strings or
   Regexp objects

 +block+::
   An optional block to be called if one of the patterns matches

 == Example

    exp = Expect.new(io)
    exp.expect('Password:') do
      send("12345")
    end