From: daz Date: 2005-11-18T01:32:19+09:00 Subject: Re: Ruby style question from a newby: exceptions YANAGAWA Kazuhisa wrote: > > irb(main):019:1> retry if c < 5 Hi Richard, Here's a variation on that theme: #-------------------------------------------------------- METHOD_Q = [ [:aaa, TypeError], [:bbb, NoMethodError], [:ccc, ArgumentError], [:ddd, NameError], ].freeze def log(item); puts "LOG: #{item}"; end def aaa; 11 + 'Two Two'; end def bbb; 12.sort; end def ccc; [].each(13) {}; end def ddd puts "4th attempt" # NOCONST # <--- uncomment end catch(:done) do stack = METHOD_Q.dup begin meth, exc = stack.shift unless meth log "Tried everything :(" throw(:done) end send(meth) log "Success at last!" rescue exc => e log "#{meth} error" log "^ #{e.message}" retry end end =begin LOG: aaa error LOG: ^ String can't be coerced into Fixnum LOG: bbb error LOG: ^ undefined method `sort' for 12:Fixnum LOG: ccc error LOG: ^ wrong number of arguments (1 for 0) 4th attempt LOG: Success at last! =end #-------------------------------------------------------- daz