# Does Ruby expect module support multiple selections?

**URL:** <https://www.ruby-forum.com/t/does-ruby-expect-module-support-multiple-selections/63329>\
**Category:** Ruby\
**Created:** [June 14, 2006, 9:29am UTC](https://www.ruby-forum.com/t/does-ruby-expect-module-support-multiple-selections/63329 "2006-06-14T09:29:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@saga](https://www.ruby-forum.com/u/saga)\
**Post date:** [June 14, 2006, 9:29am UTC](https://www.ruby-forum.com/t/does-ruby-expect-module-support-multiple-selections/63329/1 "2006-06-14T09:29:52Z")

</div>

Hi,

I want to use PTY.spawn(some shell program) and then use expect method  
to decide what should be inputed next step.  
But my shell program output is not fixed. It depends on the  
environment on which it runs. So my question is, does ruby support  
multiple expect?  
If so, my code can be like:  
PTY.spawn(my shell program) do |reader, writer, pid|  
reader.expect(/a|b/) do |str|  
if str.match(/a/)  
do something  
else  
do something else  
end  
end

Thanks very much!

---

<div class="post-metadata">

**Author:** ![saga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@saga](https://www.ruby-forum.com/u/saga)\
**Post date:** [June 14, 2006, 9:51am UTC](https://www.ruby-forum.com/t/does-ruby-expect-module-support-multiple-selections/63329/2 "2006-06-14T09:51:49Z")

</div>

On Jun 14, 2006, at 3:30 AM, Jia Zhang wrote:

> reader.expect(/a|b/) do |str|
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Well I just looked at the source, and you can do exactly this. In  
fact, expect does you one better, if you use groups in your regexp it  
yields you those two

e.g.

reader.expect(/(a|b|c)e/) do |whole\_string, first\_group,  
second\_group, etc|

```
...

```

end

---

<div class="post-metadata">

**Author:** ![saga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@saga](https://www.ruby-forum.com/u/saga)\
**Post date:** [June 14, 2006, 11:06am UTC](https://www.ruby-forum.com/t/does-ruby-expect-module-support-multiple-selections/63329/3 "2006-06-14T11:06:24Z")

</div>

Many thanks.  
My problem is solved.

Logan C. wrote:

> On Jun 14, 2006, at 3:30 AM, Jia Zhang wrote:
> 
> > reader.expect(/a|b/) do |str|
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Well I just looked at the source, and you can do exactly this. In  
> fact, expect does you one better, if you use groups in your regexp it  
> yields you those two
> 
> e.g.
> 
> reader.expect(/(a|b|c)e/) do |whole\_string, first\_group,  
> second\_group, etc|
> 
> ```
> ...
> 
> ```
> 
> end
