Ruby Command Line One Liners




The indefatigable Assaf Arkin has done it again by developing a new Ruby HTML scraping toolkit, scrAPI. Peter Szinek recently wrote a popular article about scraping from Ruby using Manic Miner, RubyfulSoup, REXML, and WWW::Mechanize, but none of these are as immediately useful as scrAPI.. so why?
This library is so amazingly cool that it requires no descriptions beyond these code examples:
Paul Battley has developed a Ruby to JavaScript converter. I'm trying to think what this is useful for, but this is an amazing results for just a few hours' work.

Ruby legend whytheluckystuff has developed a new HTML parser called Hpricot. It's easy to install and use and parses HTML in a liberal fashion. It does, however, require a compiler to install (as it's written in C), so should be okay on Linux and Mac OS X, though not necessarily on Windows (yet).
DevX.com has published a great article by Mark Watson entitled "Ruby Programming Language Enables Concise Network Programming". Mark gives quick rationales and examples for:
Flickr.rb is an 'insanely easy' Ruby library to interface with the world's most popular photo-sharing service, Flickr.
Mauricio Fernandez's ever-mindblowing Eigenclass presents a great article about how to easily implement a plugin system for any Ruby application. Ruby's reflection and OO features make it a cinch.
Paul Cantrell has created a cool guide, with demonstrations, of the powers, quirks, and surprises of closures, blocks, and procs in Ruby. He writes:
Cobra vs Mongoose is a Ruby library by Paul Battley that makes it easy to convert between XML and Ruby hashes (in both directions). It's a good alternative to YAML or JSON. It's available as a gem with gem install -r cobravsmongoose. Here's some demonstration code:
RubyNode is an interesting Ruby library that spits out semantic representations of code much in the same way the inspect method does with data. Here's a demo:

Many coders will reach a situation where developing a C extension makes sense, whether for doing 'heavy lifting', diving into assembly language, interfacing with other C code, etc. Luckily, developing a basic Ruby extension in C is easy.
Peter Szinek has announced he's going to write a series of articles on 'screen scraping' with Ruby (more accurately, extracting data from Web pages and other online sources) and has released the first article entitled "Data Extraction for Web 2.0: Screen scraping in Ruby/Rails". He covers four basic scraping techniques, first using regular expressions, then HTree and REXML, then RubyfulSoup, and finally WWW::Mechanize. If you need to process shaky HTML sources from Ruby, read on.
The RubyExamples page is a few years old now, but I just came across a great example which still works, and which demonstrates the intense power of Ruby. Please note that Justin Bishop deserves all the credit for this one.
Rubygame is a new library that provides ties between the SDL (Simple DirectMedia Layer) and Ruby, so that you can do graphics and sound work from Ruby, ultimately to build games. It's stylized off of the popular pygame, the equivalent library for Python. Unfortunately it currently only works on Linux (and other Unix-like systems) but Windows and Mac users should be in luck soon.
Steve Yegge explains how to use Ruby to script your Windows applications. His first example demonstrates how to load Internet Explorer, get it to navigate to a certain Web page, and scrape the content in just five lines of code, like so:
It's quick and easy (to do, not necessarily to parse!).
Lately I've been finding little known Ruby gems and trying them out. My latest find is EventMachine. EventMachine describes itself:
Ruby Inline is an analog to Perl's Inline::C. Out of the box, it allows you to embed C/++ external module code in your ruby script directly. By writing simple builder classes, you can teach how to cope with new languages (fortran, perl, whatever).

Inspired by the legendary (amongst Perl programmers anyway!) Perl Cookbook, comes the Ruby Cookbook. It has the equivalent examples from the Perl Cookbook but in Ruby form, of course. The basic sections are mostly complete, although some of the later sections are barely covered yet. Still, if you want to see the basic recipes for dealing with strings, numbers, dates and times, arrays, hashes, file I/O, process management, and so on, it's a cute little resource.
When using Rails it's easy to forget there's any other way. Sometimes, however, it's useful to access databases outside of that environment. Luckily, ActiveRecord can be used separately from Rails, and Craig Webster demonstrates how.
Step 1: Get the Yahoo-Ruby API. It's only a small Ruby file. Its only dependencies are net/http and REXML that come with Ruby anyway.
Bruce Williams has a great set of articles going on on his blog called "Rails Views". Each one looks at a different aspect of Rails' views and templates system and how you can use it in a cool or different way. You are bound to learn something or come up with some ideas on how to make your views more efficient (I sure have!). Here are some of the recent posts in the series:
Simon Harris laments:
An anonymous commenter contributed a cute Ruby example on this post talking about Java's verbosity. The original poster lamented on how much code you have to write to create some basic accessors on a Java class.
While looking at NegaPosi, a crazy Ruby implementation of a micro language that only uses unary operators, I discovered a cute way to initialize instance variables. Usually you'd do this: