Inspired by a posting from Hugh Sasse a couple of days ago, and
in an ongoing effort to remove languages that are in very infrequent
use on our systems, I’ve written an expect class for ruby.
It has a couple of useful features, all coming directly from Don Libes
expect, like expect_before and after, and exp_internal.
Here’s a snippet to give you a feel of how it can be used, and how
‘close’ it feels to the original, at least when using a class Expect
singleton.
The most comfortable way of using this expect is by writing a
Expect class singleton, as in the setpasswd example.
def main
$expect_debug = false
require “expectcls.rb”
expect = Expect.new
class << expect
def script(passw)
add { |x| puts “Matched #{x}” } # default action
expect_after(:EOF, :TIMEOUT) { |p| raise p.to_s }
expect(“password: “)
sleep(0.5)
send(”#{passw}\r”)
expect(“Retype”)
expect(“password: “)
sleep(0.5)
send(”#{passw}\r”)
expect(“successfully”)
end
end
expect.spawn("/usr/bin/passwd #{ARGV[0]}") do |exp|
exp.script(ARGV[1])
end
end
main
I hope this is of some interest to some people on this list.
Concrete question: there are a couple of helper classes and
modules, that should be as invisible as possible. Hoe does one go
about that? In C++ I would make a lot of stuff private, and declare
a bunch of friends, but Ruby doesn’t support those concepts.
Cheers,
Han Holl
expectcls.rb (9.72 KB)