Forum: Ruby Does Ruby expect module support multiple selections?

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.
901f3352eafdfa3e41243f06e9df56e5?d=identicon&s=25 Jia Z. (saga)
on 2006-06-14 09:29
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!
Logan Capaldo (Guest)
on 2006-06-14 09:51
(Received via mailing list)
On Jun 14, 2006, at 3:30 AM, Jia Zhang wrote:

>   reader.expect(/a|b/) do |str|
>
> --
> Posted via 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
901f3352eafdfa3e41243f06e9df56e5?d=identicon&s=25 Jia Z. (saga)
on 2006-06-14 11:06
Many thanks.
My problem is solved.

Logan Capaldo 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/.
>>
>
> 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
This topic is locked and can not be replied to.