1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/pty/lib/expect.rb
nobu 879d41e22c * ext/pty/lib/expect.rb (IO#expect): check if peer is closed.
[ruby-Bugs-17940]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15531 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-02-18 01:17:44 +00:00

36 lines
633 B
Ruby

$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