19 Apr 2007, 1:54am
/dev/ruby
by

2 comments

More Trivial Scripting with Ruby

Last Friday, in Trivial Scripting with Ruby on O'Reilly Ruby, Gregory Brown demonstrated a two-line example of the ease with which simple utility scripts can be created from Ruby. In his case a script that takes a filepath and returns the contents with all HTML entities escaped.

I like it, but I don't have any use for this operating on a per-file basis. More useful to me is being able to pass in the HTML text via command line args:

RUBY:
  1. #!/usr/bin/env ruby
  2.  
  3. require "cgi"
  4. ARGV.each { |x| puts CGI.escapeHTML( x ) }

I saved this as esc.rb. To use:

Sagarmatha:~/Documents/utilityscripts chris$ ./esc.rb "<em>Hello & World</em>"
&lt;em&gt;Hello &amp; World&lt;/em&gt;