Forum: Ruby Using Expect in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
57e7d6a3118e1943b7843d1c372d4b3b?d=identicon&s=25 stephan.zimmer (Guest)
on 2008-10-30 17:35
(Received via mailing list)
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
6d3c187a8b3ef53b08e3e7e8572c4fea?d=identicon&s=25 Jeremy McAnally (Guest)
on 2008-10-30 17:45
(Received via mailing list)
I believe Hal Fulton 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
<stephan.zimmer@googlemail.com> 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!)
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 Hugh Sasse (Guest)
on 2008-10-30 19:12
(Received via mailing list)
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/...

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....
or
http://www.koders.com/ruby/fid31E61088C10EBB90329C...

which can be found with a shorter URL

https://www.codeblog.org/viewsrc/ruby-1.8.3/ext/pt...

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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2008-10-30 19:40
(Received via mailing list)
On Oct 30, 2008, at 1:10 PM, Hugh Sasse 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 Gray II
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 Hugh Sasse (Guest)
on 2008-10-30 20:02
(Received via mailing list)
On Fri, 31 Oct 2008, James Gray 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 Gray II
>

        Hugh
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2008-10-30 20:09
(Received via mailing list)
On Oct 30, 2008, at 2:00 PM, Hugh Sasse 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 Gray II
457cf540784a12ba2f30e06565a2c189?d=identicon&s=25 Hugh Sasse (Guest)
on 2008-10-30 20:16
(Received via mailing list)
On Fri, 31 Oct 2008, James Gray 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 Gray II

        Thanks,
        Hugh
This topic is locked and can not be replied to.