From: Sam Duncan Date: 2011-10-20T05:05:28+09:00 Subject: Re: efficient way to catch & continue on exception On 20/10/11 08:45, Kassym Dorsel wrote: > C. Zona wrote in post #1027457: > http://stackoverflow.com/questions/2624835/ruby-continue-a-loop-after-catching-an-exception > > Something like that, but I can't use a loop. Each command and each hash > entry name is different and have to be hard coded. > Do they need to be hard coded? Can you configure it with a hash of name to command mapping, and then simply iterate the hash with each_pair? > irb ruby-1.9.2-p290 :001 > hs = {} => {} ruby-1.9.2-p290 :002 > hs.default = nil => nil ruby-1.9.2-p290 :003 > def command1; 'command1_result';end => nil ruby-1.9.2-p290 :004 > def command2; 'command2_result';end => nil ruby-1.9.2-p290 :005 > def command3; 'command3_result';end => nil ruby-1.9.2-p290 :006 > assignments = {:name1=>:command1, :name2=>:command2, :name3=>:command3} => {:name1=>:command1, :name2=>:command2, :name3=>:command3} ruby-1.9.2-p290 :007 > assignments.each_pair {|k,v| hs[k] = method(v).call} => {:name1=>:command1, :name2=>:command2, :name3=>:command3} ruby-1.9.2-p290 :008 > hs => {:name1=>"command1_result", :name2=>"command2_result", :name3=>"command3_result"} Then you have your loop. Sam