Using Expect in Ruby


#1

I’m using Expect inside of a ruby script to control an external
program. So far I have only used libexpect from a C program and would
have several questions regarding the Ruby interface of Expect, maybe
someone already experienced using it:

After having processed the output of the external program that I’m
interested in I would like to empty the expect buffer by expecting an
EOF marker. In an expect script this can be done by “expect eof
{ doSomething }”. Does anyone know how this can be done in Ruby?

How can I know whether the external program has already finished?

How to handle several different cases inside of an expect statement:
expect {
timeout { doAction1 }
eof { doAction2 }

}

Is there any documentation for the ruby interface?

I would also be very grateful for partial answers and/or any related
information.

Thanks in advance,

Stephan


#2

I believe Hal F. has either ported Expect to Ruby or has a nice
binding in Ruby for it. I’m not sure if he reads this list or not,
and if I didn’t see a response here in a few days, I’d just ping him
from his website.

He mentioned at the SCRubyConf in his talk. I’m not sure when those
videos will be up, but he showed some examples and such.

–Jeremy

On Thu, Oct 30, 2008 at 11:34 AM, stephan.zimmer
removed_email_address@domain.invalid wrote:

How can I know whether the external program has already finished?
I would also be very grateful for partial answers and/or any related
information.

Thanks in advance,

Stephan


http://jeremymcanally.com/
http://entp.com/
http://omgbloglol.com

My books:
http://manning.com/mcanally/
http://humblelittlerubybook.com/ (FREE!)


#3

On Fri, 31 Oct 2008, stephan.zimmer wrote:

I’m using Expect inside of a ruby script to control an external
program. So far I have only used libexpect from a C program and would
have several questions regarding the Ruby interface of Expect, maybe

I didn’t know there was one yet.

I wrote this a while[!] back:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/66218

I usually end up using Tcl to do this now, it seems to be less pain in
the end, but you may find this

http://marc.info/?l=ruby-talk&m=104759066322769&w=2

and for more normal usage of PTY + expect.rb (which can’t handle
multiple
patterns, IIRC)

http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.ruby/2008-02/msg01431.html
or
http://www.koders.com/ruby/fid31E61088C10EBB90329CB14D4306D52041F1F54F.aspx?s=thread

which can be found with a shorter URL

https://www.codeblog.org/viewsrc/ruby-1.8.3/ext/pty/expect_sample.rb

I’d love to see something better in this area. I think it would be nice
if libexpect were re-invented for ruby, so it doesn’t have the Tcl taste
when you use it. Each flavour is OK on its own, but like fish and
custard,
you don’t really want them together.

expect {
timeout { doAction1 }
eof { doAction2 }

}

In the meantime, could you drive a pure expect script with popen,
having expect_user and send_user talk to the ruby side of things?
If you have expect there already, this overburden the dependency graph
of the program, will it? And if you know how to get this working in
expect itself…

Is there any documentation for the ruby interface?

I would also be very grateful for partial answers and/or any related
information.

Thanks in advance,

Stephan

    Hugh

#4

On Oct 30, 2008, at 1:10 PM, Hugh S. wrote:

On Fri, 31 Oct 2008, stephan.zimmer wrote:

I’m using Expect inside of a ruby script to control an external
program. So far I have only used libexpect from a C program and would
have several questions regarding the Ruby interface of Expect, maybe

I didn’t know there was one yet.

expect.rb ships with Ruby.

James Edward G. II


#5

On Oct 30, 2008, at 2:00 PM, Hugh S. wrote:

maybe

I didn’t know there was one yet.

expect.rb ships with Ruby.

Correct me if I’m wrong, but that is not Don Libes’ Expect
implementation
and it has to be used with PTY. So it is not libexpect.

It’s not libexpect. You are right.

I don’t know who wrote it. It’s a very short just adding a single
method to IO. Here’s the whole thing:

$expect_verbose = false

class IO
def expect(pat,timeout=9999999)
buf = ‘’
case pat
when String
e_pat = Regexp.new(Regexp.quote(pat))
when Regexp
e_pat = pat
end
while true
if !IO.select([self],nil,nil,timeout) or eof? then
result = nil
break
end
c = getc.chr
buf << c
if $expect_verbose
STDOUT.print c
STDOUT.flush
end
if mat=e_pat.match(buf) then
result = [buf,*mat.to_a[1…-1]]
break
end
end
if block_given? then
yield result
else
return result
end
nil
end
end

I do expect that’s intended to be used with pty.

James Edward G. II


#6

On Fri, 31 Oct 2008, James G. wrote:

expect.rb ships with Ruby.
Correct me if I’m wrong, but that is not Don Libes’ Expect
implementation
and it has to be used with PTY. So it is not libexpect.

James Edward G. II

    Hugh

#7

On Fri, 31 Oct 2008, James G. wrote:

have several questions regarding the Ruby interface of Expect, maybe
I don’t know who wrote it. It’s a very short just adding a single method to
IO. Here’s the whole thing:
[…[
I do expect that’s intended to be used with pty.

OK, then my points about examples allowing multiple patterns, etc.,
stand.
I’ve been looking for something more rubyesque that does what Expect
does for tcl, for years. Doing it cross platform is nontrivial, though.

James Edward G. II

    Thanks,
    Hugh