From: RLMuller Date: 2003-05-15T04:06:43+09:00 Subject: Re: ruby_run() w/o exit? Hi, I'm a Ruby newbie, so I was interested in the techniques used in this thread. At the risk a appearing impertinent, below is the program I synthesized from the various posts on about this: If nothing else, it works nicely according to my taste :-) dummy = <<"PURPOSE" call a ruby function in a safe way. translate ruby errors into c++ exceptions. PURPOSE class MyTest class TooPainFulError < StandardError; end def initialize @langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++) end def rank lang l = lang.downcase raise NotImplementedError, lang unless i = @langs.index(l) case i when 0 raise TooPainFulError, lang else puts "#{lang} is my ##{i+1} ranked language" end end end MyTest.new.rank "C" MyTest.new.rank "NoLang" ----- Original Message ----- From: To: "ruby-talk ML" Sent: Wednesday, May 14, 2003 1:30 PM Subject: Re: ruby_run() w/o exit? > Hi -- > > On Thu, 15 May 2003, Simon Strandgaard wrote: > > > BTW: I fixed your class so that it works correctly now. > > Almost :-) > > > class Howard > > class TooPainFulError < StandardError; end > > > > def initialize > > @langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++) > > end > > > > def rank lang > > raise NotImplementedError, lang unless @langs.index lang > > You've eliminated the assignment to i, which means that this: > > > case lang > > when /[^+]$/ > > puts "#{lang} is my ##{i} ranked language" > > raises an exception. The original line: > > raise NotImplementedError, lang unless i = @langs.index lang > > didn't parse. Ara, in cases like this you might have to compromise on > the boycotting of parentheses :-) > > raise NotImplementedError, lang unless i = @langs.index(lang) > > > David > > -- > David Alan Black > home: dblack@superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > >