Expect for ruby

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)

Han Holl han.holl@pobox.com wrote in message n

Darn !

— expectcls.rb.orig Fri Mar 14 11:30:02 2003
+++ expectcls.rb Fri Mar 14 11:31:04 2003
@@ -337,7 +337,7 @@
end

def clear

  • wh.clear
  • @workingset.clear
    end

end

As long as they are created within a module, and therefore aren’t polluting
anybody’s namespace, I wouldn’t worry about it. In previous discussions on
this topic I think the majority view was that if someone wants to go through
your source and start using the internals, then the risk is on them.

Using ‘protected’ and a common base class, you can hide methods (but not
classes/modules).

I would have thought you could set ‘protected_class_method :new’ to stop
people creating instances, but this does not seem to exist in 1.6.8. Is
there an underlying reason for this?

Regards,

Brian.

···

On Fri, Mar 14, 2003 at 06:23:35AM +0900, Han Holl wrote:

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.

I would have thought you could set 'protected_class_method :new' to stop

Well ,just use

   class << self
      protected
      def new
         # ...
      end
   end

Guy Decoux