On Thu, 2006-02-02 at 09:57 +0900, Michael Judge wrote: > Does this seem like a good way to approach it? > 1. Store each command's matching regular expression and ruby code > within the database. (sample fixture below) > 2. For each line in script: > Test line against each command's corresponding regular > expression > If matched, execute the command's ruby code using an > instance_eval. > > My thoughts are: > 1. Storing executable code in the database is a security problem > 2. instance_eval is slow > 3. The alternative (a big if/elsif tree) would span many pages and > be unweildy. > > Have a better suggestion? Not necessarily better, but how about something like: class Commands def Q(args) puts args end def X(args) if args =~ /^(\d+) (.*)$/ puts " o #{$2}" end end def M(args) if args =~ /^(\d+) (.*)$/ puts " [ ] #{$2}" end end def dispatch(line) if line =~ /([QXM])-?(.*)/ send($1.intern, $2) else raise "Invalid input: #{line}" end end end s = <<EOS Q Just a label X-23 Single punched M-11 Multi punched J-12 Bad input EOS cmds = Commands.new s.each { |c| cmds.dispatch(c) } (Obviously I guessed a bit with the input format). Output: Just a label o Single punched [ ] Multi punched -:22:in `dispatch': Invalid input: J-12 Bad input (RuntimeError) from -:35 from -:35 -- Ross Bamford - rosco / roscopeco.REMOVE.co.uk