James Gray
9/24/2007 10:30:00 PM
On Sep 24, 2007, at 5:18 PM, Cameron Matheson wrote:
> Hi,
>
> On 9/24/07, yermej@gmail.com <yermej@gmail.com> wrote:
>> If you need to interact with something (a la irb, ftp, telnet, etc.)
>> you'll need to find something else, I think.
>
> This is what I'm looking for (something to interact w/ a program that
> has an ftp-like interface).
For FTP use Net::FTP, of course. Otherwise, Ruby does ship with a
simple expect library:
$ cat /usr/local/lib/ruby/1.8/expect.rb
$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).nil? 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
__END__
Hope that helps.
James Edward Gray II