From: matz@... (Yukihiro Matsumoto) Date: 2000-03-07T11:01:10+09:00 Subject: [ruby-talk:01767] Re: Expect for Ruby Hi, In message "[ruby-talk:01764] Expect for Ruby" on 00/03/06, "Dat Nguyen" writes: |Is there Expect for Ruby, or the functionality of Expect can be done |straight in Ruby. I don't know expect well enough, but this class from my personal library may help. Usage: expect = Expecter.new expect.on(/remote IP address/) do .. do things for the pattern comes ... end expect.on(/[^n]REDIAL ERROR/) do exit end expect.wait(f) Code follows: class Expecter def initialize(timeout=nil, dynamic=nil) @timeout=timeout @action= {} @dynamic=dynamic end def on(pattern, action=lambda) @action[pattern] = action end def wait(on) keys = @action.keys while true # fds = select([on], nil, nil, @timeout) fds = select([on], nil, nil) unless fds if @action['TIMEOUT'] @action['TIMEOUT'].call end return end break if fds.size == 0 or fds[0].size == 0 line = on.gets break unless line print line for key in keys if line =~ key @action[key].call(line) end end keys = @action.keys if @dynamic end if @action['EOF'] @action['EOF'].call end end def expect(on) fds = select([on], nil, nil, @timeout) unless fds if @action['TIMEOUT'] @action['TIMEOUT'].call end return end return if fds.size == 0 or fds[0].size == 0 line = on.gets unless line if @action['EOF'] @action['EOF'].call end return end print line for key in @action.keys if line =~ key @action[key].call(line) end end end end