On Thu, May 16, 2002 at 07:05:27AM +0900, John Carter wrote:
> I'm busy rewriting expect.rb to handle things in bigger chunks. I'm

Which expect.rb is it that you are rewriting?

> using it on a slow '486 driving 7 different hardware things lurking at the
> end of 7 serial ports. I'm rewriting expect to use one Thread, and a
> single select for all ports and sysread instead of getch.

In Ruby, when multiple threads are waiting on IO, the interpreter makes
a single call to select() internally.  I'm not sure if there will be
much of a performance benefit in single thread/one select over multiple
threads.

> Things are progressing quite well except when things go wrong in a Thread,
> then all I know about it is something doesn't work. No error message,
> stack trace nothing.
> 
> Is there a way of convincing ruby to do like Java...
>  try {
>    // something that may die messily
>    .
>    .
>  } catch (Exeception e) {
>    e.printStackTrace();
>  }

I don't know of a good way to print a stack trace in Ruby, though a nice
interface to eval.c's error_print would certainly be nice.  As an
alternative, you can do this:

  def exception_str(exc=$!)
    str = ''
    first = true
    exc.backtrace.each do |bt|
      str << (first \
        ? "#{bt}: #{exc.message} (#{exc.type})\n" \
        : "\tfrom #{bt}\n")
      first = false
    end
    str
  end

  def print_exception(exc=$!, out=$stdout)
    out.puts exception_str(exc)
  end

  begin
    # ...
  rescue Exception
    print_exception()
  end

Paul