Begin forwarded message: > From: Alex Wayne <alex / beautifulpixel.com> > Date: November 20, 2006 7:15:26 PM CST > To: submission / rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > My first ruby quiz entry, since it seems to be an easy week. Not > as complicated as some of the other solutions, but gets the job > done without much fuss. > > Although I didn't peek, it ended up being very similar to http:// > blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/225745, but I > would like to think mine is a bit easier to read an understand. > > Keep up the great quizzes, > -Alex Wayne > > > > Usage: > ruby lrb.rb some_file > > Program code: > > # Get filename > filename = ARGV.first > filename += '.lrb' unless filename =~ /\.\w+$/ > > # load source > source = File.read(filename) > > # Initialize variables for loop > inside_block = false > executable_lines = [] > > # Process file, line by line > source.each do |line| > # Look for the start of a code block > if !inside_block && line =~ (/^\\begin\{(\w+)\}/) > inside_block = Regexp.last_match[1] > next > end > # Look for the end of a code block, with the same identifier that > opened it > if inside_block && line =~ /^\\end\{#{inside_block}\}/ > inside_block = false > next > end > if inside_block > # Inside a code block so simply add the lines to the stack > executable_lines << line > else > # Trim off the '>' and add the line to the stack > executable_lines << line.gsub(/^>\s/, '') if line =~ /^>\s/ > end > end > > # Convert array of lines into a single string executable_lines = > executable_lines.join("\n") > > # Execute the resulting code > eval(executable_lines)