# How to use conditionals with expect.rb?

**URL:** <https://www.ruby-forum.com/t/how-to-use-conditionals-with-expect-rb/109754>\
**Category:** Ruby\
**Created:** [July 18, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/how-to-use-conditionals-with-expect-rb/109754 "2007-07-18T17:15:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyoncrack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubyoncrack](https://www.ruby-forum.com/u/rubyoncrack)\
**Post date:** [July 18, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/how-to-use-conditionals-with-expect-rb/109754/1 "2007-07-18T17:15:27Z")

</div>

This is a shot in the dark since it appears nobody uses expect.rb.

Does anybody know how to use a conditional with IO.expect("")?

Everytime I enter a string that isn’t expected into IO.expect(""), the  
script just hangs and I can’t do anything with the failure.

---

<div class="post-metadata">

**Author:** ![rubyoncrack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubyoncrack](https://www.ruby-forum.com/u/rubyoncrack)\
**Post date:** [July 18, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/how-to-use-conditionals-with-expect-rb/109754/2 "2007-07-18T20:34:42Z")

</div>

Kazushi S. wrote:

> This is a shot in the dark since it appears nobody uses expect.rb.
> 
> Does anybody know how to use a conditional with IO.expect("")?
> 
> Everytime I enter a string that isn’t expected into IO.expect(""), the  
> script just hangs and I can’t do anything with the failure.

Well, at least RExpect has the ability to timeout when an unexpected  
string is parsed. The verbose and log file absolutely bite though.

---

<div class="post-metadata">

**Author:** ![rubyoncrack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubyoncrack](https://www.ruby-forum.com/u/rubyoncrack)\
**Post date:** [July 18, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/how-to-use-conditionals-with-expect-rb/109754/3 "2007-07-18T20:39:39Z")

</div>

On 7/18/07, Kazushi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is a shot in the dark since it appears nobody uses expect.rb.
> 
> Does anybody know how to use a conditional with IO.expect(“”)?
> 
> Everytime I enter a string that isn’t expected into IO.expect(“”), the  
> script just hangs and I can’t do anything with the failure.

Can you post your script? I believe the point of expect is to just  
keep accepting input till it gets the “expected” value - try entering  
the right string after you enter the wrong one. Here’s a sample  
interaction with expect - I’m assuming ruby expect follows the same  
behaviour:

martin@dabba ~ $ cat hi.exp  
set timeout -1  
expect “hello world\n”  
send “we get signal!\n”

martin@dabba ~ $ expect hi.exp  
how are you gentlemen?  
somebody set up us the bomb  
move zig!!!  
hello world  
we get signal!

martin@dabba ~ $

martin

---

<div class="post-metadata">

**Author:** ![rubyoncrack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubyoncrack](https://www.ruby-forum.com/u/rubyoncrack)\
**Post date:** [July 20, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/how-to-use-conditionals-with-expect-rb/109754/4 "2007-07-20T17:44:53Z")

</div>

Martin DeMello wrote:

> Can you post your script? I believe the point of expect is to just  
> keep accepting input till it gets the “expected” value - try entering  
> the right string after you enter the wrong one. Here’s a sample  
> interaction with expect - I’m assuming ruby expect follows the same  
> behaviour:
> 
> martin@dabba ~ $ cat hi.exp  
> set timeout -1  
> expect “hello world\n”  
> send “we get signal!\n”
> 
> martin@dabba ~ $ expect hi.exp  
> how are you gentlemen?  
> somebody set up us the bomb  
> move zig!!!  
> hello world  
> we get signal!
> 
> martin@dabba ~ $
> 
> martin

There’s no way to enter the correct Expect string since it hangs. Also,  
don’t even think of using RExpect. “RExpect is better than you’d  
expect” my ass.
