More Trivial Scripting with Ruby

   By chris on April 19th 2007 in /dev/ruby | 442 views

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;

2 Responses to “More Trivial Scripting with Ruby”

  1. Gregory responded on 24 Apr 2007 at 11:00 am #

    Neat. You can also pass stuff on STDIN to the script I wrote.

    $ echo “Foo” | ruby -rcgi -e “puts CGI.escapeHTML(ARGF.read)”
    <br>Foo</br>

  2. Gregory responded on 24 Apr 2007 at 11:00 am #

    oh, the formatting on that got messed up sadly.

Trackback URI | Comments RSS

Leave a Reply