From: Jacob Fugal Date: 2005-09-10T04:46:55+09:00 Subject: Re: Dynamic method execution problem On 9/9/05, joe.yakich@gmail.com wrote: > # The ie variable here refers to a Watir::IE object > begin > my_method = methods.shift > puts "Trying #{my_method}" > eval "#{my_method}(ie)" > end > > rescue UnknownObjectException > if methods.length > 0 > retry > else > raise > end First, a fix... you don't want the end to the begin block until after the rescue, example: begin # do risky operation rescue # recover from failure end Then, translating to use send instead of eval: begin my_method = methods.shift puts "Trying #{my_method}" send(my_method.to_sym, ie) rescue UnknownObjectException retry unless methods.empty? raise end (I also changed the content of the rescue clause to something that flows a bit nicer for me, but this is just a style issue. What you had works fine) The send method sends the method identified by the first argument to the receiver (here the implicit self) with the remainder of the arguments. Myself, rather than using retry, I'd do something like this: methods = [ :method1, :method2, ... ] methods.find do |method| puts "Trying #{method}" begin send( method, ie ) method rescue end end or raise "No applicable method found" This makes the nature of looping through the methods more explicit, rather than relying on a combination of .shift, .empty? and retry. Jacob Fugal