[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6436] Re: Two-Way Pipes with popen

From: matz@... (Yukihiro Matsumoto)
Date: 2000-11-18 05:11:26 UTC
List: ruby-talk #6436
Hi.

In message "[ruby-talk:6431] Two-Way Pipes with popen"
    on 00/11/18, jweirich@one.net <jweirich@one.net> writes:

|I want to start a process and feed it information through its standard
|input and read the result from its standard output.  The "popen"
|command seems to do one or the other, but not both at once.  It there
|a way to do this?  (Currently I'm using a work-around using a
|temporary file).

Try open3 library.

  a = Open3.popen3("nroff -man")
  Thread.start do
    while line = gets
      a[0].print line
    end
    a[0].close
  end
  while line = a[1].gets
    print ":", line
  end

|In a similar vein, is there an "expect"-like addon for Ruby?

I'm using a stuff like this:

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)
      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

expect = Expecter.new
expect.on(/remote IP address/) do
  print "connection established\n"
  exit
end

expect.on(/[^n]REDIAL ERROR/) do
  exit
end

f = open("|tail -n 0 -f /var/log/messages")
expect.wait(f)

In This Thread