Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Saturday, February 19, 2005

Ruby for Object-Oriented Shell Scripts

Matz brilliantly included backticks (`) in Ruby as a low-profile way to integrate system commands into a Ruby script. This means that Ruby can be used to create powerful object-oriented shell scripts. Now you never have to write a batch file or bash script again!

Below is an example that queries Slashdot for its top 3 headlines every hour. Note the cool Ruby string substitution on the "count" parameter.

class Slashdot
def headlines(count)
`lynx -source "" | grep "</A>: " | sed "s#.*</A>: \\(.*\\)</B>.*#\\1#" | head --lines=#{count}`

while true
`sleep 1h`


  • Wrapping shell scripts in Objects does not make it any more OO, than wrapping binary data in two xml-tags makes it well-structured sematic data. ;)

    By Anonymous Anonymous, at 2/20/2005 11:10 a.m.  

  • You gotta admit though that it's more fun to write a Ruby script that a bash script.

    By Blogger Jonathan, at 2/20/2005 12:33 p.m.  

Post a Comment

<< Home