From: Daniel Moore Date: 2009-09-08T07:36:39+09:00 Subject: [QUIZ][SUMMARY] Syntax Highlighting (#218) This week's quiz was solved by Caleb Clausen. Caleb used the `RubyLexer` gem to parse a Ruby file given as a command line argument. `Term::ANSIColor` is used to color the tokens. def coloruby file,fd=open(file) lexer=RubyLexer.new(file,fd) begin token=lexer.get1token print token.colorize end until RubyLexer::EoiToken===token ensure print Term::ANSIColor.reset end The `coloruby` method uses `RubyLexer` to generate a stream of tokens. Each token is then colorized and printed out. After all the tokens are printed the color is reset with `Term::ANSIColor.reset` so that any following text won't receive collateral colorization. So how are these tokens colorized anyway? Caleb's solution opens up the `RubyLexer` class and adds a `colorize` method to all tokens. `Term::ANSIColor` is also included in the `Token` class so that all the color methods are available as well. class Token include Term::ANSIColor def colorize color+ident.to_s end end Each individual token class may define its own different color or colorize method. class MethNameToken alias color green end class KeywordToken def colorize if /[^a-z]/i===ident yellow+ident else red+ident end end end The end result is nice colorful Ruby code.[1] Thank you Caleb for your solution to this week's quiz! [Syntax Highlighting (#218) - Solutions][2] [1]: http://rubyquiz.strd6.com/quizzes/218/colorized.png [2]: http://rubyquiz.strd6.com/quizzes/218.tar.gz