require 'pty'
PTY.spawn("ls") { |r, w, pid| puts r.readlines }
...leads to failure with the exception PTY::ChildExited.
The most infuriating thing is that, despite not knowing what
I am doing, I once had a Ruby expect script which invoked
a function in Pari/gp and took the string result back into
Ruby for further processing. This script no longer works
correctly (it gives answers, but wrong answers). I can't
figure out what is wrong because I don't understand PTY and
expect and I can't find functioning examples or useful
documentation anywhere (the rdoc output is not useful documentation
here). Any pointers to explanations and examples that actually
work will be greatly appreciated. Thanks!
Regards, Bret
try this (untested on 1.9.0 but the example didn't work with 1.8.1 either -
this does):
#
# sample program of expect.rb
#
# by A. Ito
#
# This program reports the latest version of ruby interpreter
# by connecting to ftp server at netlab.co.jp.
#
require 'pty'
require 'expect'
uri = ARGV.shift || "ftp.ruby-lang.org"
STDOUT.sync = true
STDERR.sync = true
$expect_verbose = true
username = ENV['USER'] || ENV['LOGNAME'] || username = 'guest'
versions = []
login_pat = %r/^\s*name.*:\s*/io
password_pat = %r/^\s*password:/io
prompt_pat = %r/^\s*ftp\s*>\s*/io
version_pat = %r/ruby-(\d\.\d.\d)\.tar\.gz/io
PTY.spawn("ftp ftp.ruby-lang.org") do |r_f,w_f,pid|
w_f.sync = true
r_f.expect(login_pat){ w_f.puts "ftp" }
r_f.expect(password_pat){ w_f.puts "#{ username }@" }
r_f.expect(prompt_pat){ w_f.puts "cd pub/ruby" }
r_f.expect(prompt_pat){ w_f.puts "dir" }
r_f.expect(prompt_pat) do |output|
output.first.each do |line|
m = version_pat.match(line)
versions.push m[1] if m
end
end
w_f.print("bye") rescue nil
end
puts versions.join ','
puts "The latest ruby interpreter is <#{ versions.sort.last }>"
-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| "640K ought to be enough for anybody." - Bill Gates, 1981
===============================================================================
That didn't work for me either. Looking at it with ruby -rdebug
shows that the variable called output contained just an ftp prompt
rather than the directory listing. Hence versions remained an
empty array and versions.sort.last was nil, resulting in the output
line:
The latest ruby interpreter is <>
Regards, Bret