# Using Expect in Ruby

**URL:** <https://www.ruby-forum.com/t/using-expect-in-ruby/152159>\
**Category:** Ruby\
**Created:** [October 30, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159 "2008-10-30T17:35:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephan.zimmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@stephan.zimmer](https://www.ruby-forum.com/u/stephan.zimmer)\
**Post date:** [October 30, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159/1 "2008-10-30T17:35:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephan.zimmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@stephan.zimmer](https://www.ruby-forum.com/u/stephan.zimmer)\
**Post date:** [October 30, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159/2 "2008-10-30T17:45:15Z")

</div>

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](mailto: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://jeremymcanally.com/)  
[http://entp.com/](http://entp.com/)

> **[Omg GIFs | GIFDB.com](https://gifdb.com/omg)**
>
> Download Omg GIFs for Free on GifDB. More than 56 Omg Animated GIFs to download.

My books:

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

[http://humblelittlerubybook.com/](http://humblelittlerubybook.com/) (FREE!)

---

<div class="post-metadata">

**Author:** ![stephan.zimmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@stephan.zimmer](https://www.ruby-forum.com/u/stephan.zimmer)\
**Post date:** [October 30, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159/3 "2008-10-30T19:12:04Z")

</div>

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](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](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](http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.ruby/2008-02/msg01431.html)  
or  
[http://www.koders.com/ruby/fid31E61088C10EBB90329CB14D4306D52041F1F54F.aspx?s=thread](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](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

```

---

<div class="post-metadata">

**Author:** ![stephan.zimmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@stephan.zimmer](https://www.ruby-forum.com/u/stephan.zimmer)\
**Post date:** [October 30, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159/4 "2008-10-30T19:40:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephan.zimmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@stephan.zimmer](https://www.ruby-forum.com/u/stephan.zimmer)\
**Post date:** [October 30, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159/5 "2008-10-30T20:09:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephan.zimmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@stephan.zimmer](https://www.ruby-forum.com/u/stephan.zimmer)\
**Post date:** [October 30, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159/6 "2008-10-30T20:02:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephan.zimmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@stephan.zimmer](https://www.ruby-forum.com/u/stephan.zimmer)\
**Post date:** [October 30, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/using-expect-in-ruby/152159/7 "2008-10-30T20:16:40Z")

</div>

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