[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01767] Re: Expect for Ruby

From: matz@... (Yukihiro Matsumoto)
Date: 2000-03-07 02:01:10 UTC
List: ruby-talk #1767
Hi,

In message "[ruby-talk:01764] Expect for Ruby"
    on 00/03/06, "Dat Nguyen" <thucdat@hotmail.com> 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

In This Thread

Prev Next