From: Ross Bamford Date: 2006-02-02T11:15:46+09:00 Subject: Re: Grammars (mini-scripting languages) 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 = <