# Expect, rexpect and others

**URL:** <https://rubytalk.org/t/expect-rexpect-and-others/9268>\
**Category:** ruby-talk\
**Created:** [9 December 2003 23:36 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268 "2003-12-09T23:36:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Giddings1](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Ben\_Giddings1](https://rubytalk.org/u/Ben_Giddings1)\
**Post date:** [9 December 2003 23:36 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/1 "2003-12-09T23:36:19Z")

</div>

I know that now Ruby includes “expect”, and that’s great, however I noticed  
that it reads data one character at a time, I’m trying to use it on  
something that spits out a _lot_ of data, and don’t think this is the best  
solution to the problem.

I then came across this page:

[http://www.rubygarden.org/ruby?RExpect](http://www.rubygarden.org/ruby?RExpect)

It attempts to fix some of the perceived flaws with expect, including  
getting things one character at a time.

Does anybody have any experience with either, and have any suggestions to  
someone wanting this kind of functionality?

Ben

---

<div class="post-metadata">

**Author:** ![Ben\_Giddings1](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Ben\_Giddings1](https://rubytalk.org/u/Ben_Giddings1)\
**Post date:** [10 December 2003 23:45 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/2 "2003-12-10T23:45:03Z")

</div>

Ben Giddings wrote:

> [http://www.rubygarden.org/ruby?RExpect](http://www.rubygarden.org/ruby?RExpect)
> 
> It attempts to fix some of the perceived flaws with expect, including  
> getting things one character at a time.

Since I had no answers, let me ask a few more questions. Having looked at  
the RExpect stuff, it looks much better than the standard ‘expect’ library,  
mainly because it isn’t character based. So, has anybody used it? Does  
anybody know if it exists in any form other than a web page?

Ben

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [11 December 2003 04:54 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/3 "2003-12-11T04:54:45Z")

</div>

Slightly unfair of me to comment since I wrote it…

But yes, I have used it to drive very many (100’s) of real and virtual  
devices simultaneously. So yes, it is robust and does scale, which I  
found the standard one didn’t.

That’s not quite the absolute latest version I have but should be good  
to try. (The API hasn’t changed except I now through a TimeoutError  
instead of EOFError on timeout.)

RubyForge didn’t exist when I submitted it, I guess I should create a  
RubyForge project for it. Except it is so small it is more appropriate  
to included it as part of some other project.

Is there a “Standards Track” library project for Ruby  
like the [http://www.boost.org](http://www.boost.org) library for C++?

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

A Million Monkeys can inflict worse things than just Shakespeare on  
your system.

> **···**
>
> On Thu, 11 Dec 2003, Ben Giddings wrote:
> 
> > Ben Giddings wrote:
> > 
> > > [http://www.rubygarden.org/ruby?RExpect](http://www.rubygarden.org/ruby?RExpect)
> > > 
> > > It attempts to fix some of the perceived flaws with expect, including  
> > > getting things one character at a time.
> > 
> > Since I had no answers, let me ask a few more questions. Having looked at  
> > the RExpect stuff, it looks much better than the standard ‘expect’ library,  
> > mainly because it isn’t character based. So, has anybody used it? Does  
> > anybody know if it exists in any form other than a web page?

---

<div class="post-metadata">

**Author:** ![Ben\_Giddings1](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Ben\_Giddings1](https://rubytalk.org/u/Ben_Giddings1)\
**Post date:** [11 December 2003 19:12 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/4 "2003-12-11T19:12:12Z")

</div>

John Carter wrote:

> Slightly unfair of me to comment since I wrote it…
> 
> But yes, I have used it to drive very many (100’s) of real and virtual  
> devices simultaneously. So yes, it is robust and does scale, which I  
> found the standard one didn’t.

Great!

> That’s not quite the absolute latest version I have but should be good  
> to try. (The API hasn’t changed except I now through a TimeoutError  
> instead of EOFError on timeout.)

Ok, a few questions then:

Is there some historical reason why the result of a match is “buffer”, $1,  
$2, $3…? To me, it would make more sense to do

matchdata = regexp.match(buffer)

return matchdata.to\_a

I don’t see why it is useful to return the whole buffer, and also this  
allows really complex regexps with more than 9 elements.

Also, the other efficiency thing that concerns me is that both versions  
attempt to match against the entire buffer. The majority of regexps  
_could_ match something huge, but are generally only used for something  
less than say 80 chars long. I want to use an ‘expect’ type library  
against something that is updating a count of each byte of a 200k+ file  
written. After that progress meter it prints “Done.”, which is all I  
really want to match against. It would be more efficient if there were an  
option to specify the maximum size of the buffer to use as a match, and to  
throw out parts of a buffer bigger than that. Something like:

# Append the data to the match buffer, and shorten it if necessary

match\_buff \<\< read\_buff  
if match\_buff.length \> maxlength  
match\_buff = match\_buff[-maxlength .. -1]  
end  
debug(“match buff is now \<\<#{match\_buff}\>\>”)

> RubyForge didn’t exist when I submitted it, I guess I should create a  
> RubyForge project for it. Except it is so small it is more appropriate  
> to included it as part of some other project.

It really isn’t all that small, I mean, look at most of the ruby libs,  
they’re much smaller. I think it’s worth adding to the stdlib.

Ben

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard1](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Simon\_Strandgaard1](https://rubytalk.org/u/Simon_Strandgaard1)\
**Post date:** [11 December 2003 19:21 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/5 "2003-12-11T19:21:59Z")

</div>

> Is there some historical reason why the result of a match is “buffer”, $1,  
> $2, $3…? To me, it would make more sense to do

I think $1…$9 are inspired by perl…

> matchdata = regexp.match(buffer)

Yes that makes much more sense.

BTW: I am working on a regexp engine written entirely in Ruby.  
[http://raa.ruby-lang.org/list.rhtml?name=regexp](http://raa.ruby-lang.org/list.rhtml?name=regexp)

> **···**
>
> On Fri, 12 Dec 2003 04:12:12 +0900, Ben Giddings wrote:
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Jamis\_Buck4](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jamis\_Buck4](https://rubytalk.org/u/Jamis_Buck4)\
**Post date:** [11 December 2003 19:37 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/6 "2003-12-11T19:37:19Z")

</div>

Ben Giddings wrote:

> Is there some historical reason why the result of a match is “buffer”,  
> $1, $2, $3…? To me, it would make more sense to do
> 
> matchdata = regexp.match(buffer)
> 
> return matchdata.to\_a

The other thing I would really like to see in this library is the  
ability to specify an IO stream that will display the output of the  
process… Perhaps there already is a way, but couldn’t see it. What I  
did was add an output\_stream= setter, and then in the inputData routine  
I just sent ‘data’ to the output stream, if it was set.

Does the library already support this functionality? Is there a better  
way to do this? (I’m thinking along the lines of the ‘interact’ feature  
of the python ‘pexpect’ library.)

> **···**
>
> –  
> Jamis Buck  
> jgb3@email.byu.edu
> 
> ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S\*)|le.l(…)e|#!(\S\*)/) {|r| a \<\< r.compact.first };puts “\n\>#{a.join(%q/ /)}\<\n\n”’

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [11 December 2003 21:48 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/7 "2003-12-11T21:48:24Z")

</div>

Ok, I’m setting up a RubyForge project for it, anyone interested in  
being co-developers on it?

> Is there some historical reason why the result of a match is “buffer”, $1,  
> $2, $3…? To me, it would make more sense to do

Well I started it as a drop in replacement for the one in the Ruby  
standard library.(Which is admirably brief and elegant), I already had  
code that was using that, but it was too slow.

I think at one stage they did that. I see now they have  
if mat=e\_pat.match(buf) then  
result = [buf,\*mat.to\_a[1…-1]]

One major API change relative to the standard expect.rb is the timeout  
is the number of seconds the select blocks waiting for input.

So if the timeout is 2 seconds and single character trickles in every  
second you never ever timeout. In RExpect I timeout if I haven’t seen  
a match in timeout seconds.

> Also, the other efficiency thing that concerns me is that both versions  
> attempt to match against the entire buffer. The majority of regexps  
> _could_ match something huge, but are generally only used for something  
> less than say 80 chars long. I want to use an ‘expect’ type library  
> against something that is updating a count of each byte of a 200k+ file  
> written. After that progress meter it prints “Done.”, which is all I  
> really want to match against. It would be more efficient if there were an  
> option to specify the maximum size of the buffer to use as a match, and to  
> throw out parts of a buffer bigger than that. Something like:

Once I have it checked into Rubyforge you are welcome to hack and  
patch it and either join me as a developer with CVS access rights or  
just send me patches. My only request is to try keep the API  
reasonably backward compatible, either by use of default parameters or  
providing new interfaces.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

A Million Monkeys can inflict worse things than just Shakespeare on  
your system.

> **···**
>
> On Fri, 12 Dec 2003, Ben Giddings wrote:

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [11 December 2003 21:50 UTC](https://rubytalk.org/t/expect-rexpect-and-others/9268/8 "2003-12-11T21:50:07Z")

</div>

I’m totally unfamiliar with pexpect, once I have commented RExpect and  
checked it into RubyForge, will you compare with pexpect and make any  
suggestions (or just hack them in your self)…

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

A Million Monkeys can inflict worse things than just Shakespeare on  
your system.

> **···**
>
> On Fri, 12 Dec 2003, Jamis Buck wrote:
> 
> > Does the library already support this functionality? Is there a better  
> > way to do this? (I’m thinking along the lines of the ‘interact’ feature  
> > of the python ‘pexpect’ library.)
