Home | Ruby | Ruby Script Beautifier |     Share This Page
Ruby Script Beautifier
All content Copyright © 2007, P. LutusMessage Page

Version 2.9, 10/24/2008


I have always felt that a language hasn't really arrived until there is a source beautifier utility for it.

So, failing to find a Ruby beautifier online, I decided to write one.

Those programmers who have experience with source code beautifiers will know they don't handle all cases — there is always a borderline case that the beautifier won't know about, preventing it from indenting the source perfectly. That rule is certainly true in this case, since I have only tested this script on perhaps thirty Ruby scripts, most from the standard Ruby library, as well as some of my own. At the present stage of development, the script correctly beautifies all the source files I have fed it.

I am certain that people will find scripts that this beautifier won't fully understand, and I hope they will send along the problem scripts so I can try to improve the algorithm as time passes. There are some perverse aspects to Ruby's syntax that makes beautification somewhat tricky.

This Ruby beautifier is itself written in Ruby, and it is surprisingly short. It will undoubtedly get longer as time passses and its ability to handle borderline cases improves.
  • Click here to see a Web page with the Ruby script.
  • Click here for a plain text file containing the script.
  • Examine the script listing. Note the arrays named "IndentExp" and "OutdentExp". These lists of regular expressions are responsible for most of the program's logic.
  • This script accepts a command-line list of file names, creates a backup copy of each named file and beautifies the originals.
  • The script can also be used as part of a stream (note the "-" argument): (source) | (script_name) - > output_file
  • If there is an indentation error (an imbalance of opening and closing keywords), the script will print an error message but will save the result anyway.
  • Indentation errors that appear in working scripts are obviously the fault of the beautifier, not the script. I would appreciate hearing about such errors, with details, so I can improve the beautifier's logic.

Revision History
  • Version 2.9 10/24/2008. Restored some error printing code that was unwisely taken out.
  • Version 2.8 10/22/2008. Arranged to return an error code in the event of mismatched indent/outdent, to simplify automation.
  • Version 2.7 10/22/2008. Cleaned up a few style problems, increased the robustness of the here-doc test code.
  • Version 2.6 10/21/2008. Added code to pass here-doc content unchanged.
  • Version 2.5 10/09/2008. Fixed a misnamed function call.
  • Version 2.4 08/25/2008. Added code to distinguish between loading as a module and launching as a standalone program.
  • Version 2.3 08/18/2008. Rewrote the program as a Ruby module for greater flexibility.
  • Version 2.2 10/29/2007. Added code to avoid scanning within comments that appear at the end of lines.
  • Version 2.1 09/11/2006. Made more changes to the filtering code.
  • Version 2.0 09/06/2006. Revised code to improve the beautifier's ability to deal with special cases.
  • Version 1.9 09/01/2006. Reworked the filtering code yet again.
  • Version 1.8 08/31/2006. Dealt with backtick-delimited sequences.
  • Version 1.7 08/24/2006. Corrected bad handling of a particular form of "case".
  • Version 1.6 08/19/2006. Changed pre-indentation line filtering.
  • Version 1.5 08/12/2006. Added code to properly handle the Ruby program-end mark "__END__".
  • Version 1.4 08/11/2006. Corrected a bug that failed to sort out sequences of forward slashes.
  • Version 1.3 04/03/2006. Corrected a bug having to do with entering and exiting Ruby comment blocks.
  • Version 1.2 03/31/2006. Refactored the code that deals with extended lines.
  • Version 1.1 03/30/2006. After more testing, revised the algorithm to accommodate a larger variety of Ruby scripts.
  • Version 1.0 03/29/2006. Initial Public Release.
This script beautifier is Copyright 2008, P. Lutus, and is released under the GPL.

Home | Ruby | Ruby Script Beautifier |     Share This Page