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:
-
#!/usr/bin/env ruby
-
-
require "cgi"
-
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>"
<em>Hello & World</em>

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>
Gregory responded on 24 Apr 2007 at 11:00 am #
oh, the formatting on that got messed up sadly.