Ned Konz wrote:
> I just started looking at Ruby (reading Dave Thomas' book), and I'm puzzled 
> as to why you have both rescue/raise  and catch/throw. I can't see what the 
> distinction is, other than somewhat different syntax. Are they used for 
> different purposes?

Yes, they are used for different purposes.

Rescue is a keyword, raise a method. They are used for exception handling, in the same
way as in C++ catch or throw. Thus they are use to indicate an error condition.

catch/throw are two methods, and are used to escape from nested loops, i.e. to control the 
flow. In Java, IIRC, there is "break 'label'", which lets you escape from nested loops:

  loop1: for(i=1; i<=10; i++) {
    while (a<b) {
      if (i==10) break loop1;
    }
  }

The same in Ruby:

  catch(:loop1) {
    for i in 1..10
      while(a < b)
        throw :loop1 if i == 10  
      end
    end
  } 


> 
> Also, I note with some disappointment that Ruby doesn't seem to have 
> "resume" semantics on its exceptions like Smalltalk does. I'm coming to 
> Ruby from Smalltalk and Perl, and though Ruby's exceptions are obviously 
> much better than Perl's, they don't seem to offer some of the strengths of 
> Smalltalk's.

Don't know about "resume", but perhaps "retry" is what you want.
"retry" executes the block that failed once again.


> One of the reasons that resume semantics are interesting in Smalltalk is 
> that you can (for instance) pop up a debugger on an exception, interact 
> with the system, and then resume execution of the program.

That should be possible with Ruby, too.

> I guess that Ruby isn't yet aimed at the kind of long-running and 
> interactive systems that Smalltalk excels at. For instance, I used to write 
> factory machine control software in Smalltalk. Without stopping the 
> machine, we could load new versions of methods into the system, stop 
> threads, debug them, and resume them, and otherwise maintain the system 
> without bringing it down.

Possible with Ruby, too.
Ruby is as dynamic as Smalltalk is.
No problem to load new methods at runtime, etc..

> Does Ruby offer this ability? It seems that the debugger hooks are in an 
> inconvenient place (via a command-line option); does this mean that one 
> cannot debug a running system that has not been started with the debug 
> flag? I realize that you could have a thread in which you could do evals, 
> but can you (for instance) pause a running thread, step through it a bit, 
> and then resume it?

Smalltalk runs a debugger by default (I guess). Ruby do not.
That's because Smalltalk is mostly an IDE + language, where Ruby is just an interpreter.

But it should be possible to load a debugger at runtime. Just call anywhere in a Ruby program
"require debug.rb". That should do it.


Regards,

  Michael

-- 
Michael Neumann
merlin.zwo InfoDesign GmbH
http://www.merlin-zwo.de