Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Author Archives: Ric Roberts

By Ric Roberts / July 20, 2009

rackFrançois Vaux has recently published a Ruby module called Rackable which allows you to make any Ruby object Rack-friendly, providing it with a REST-like interface.

What does this mean? Well, a Rack application is essentially a Ruby object that responds to call(). Rackable just gives your object a call method which uses the Rack environment to dispatch to a method.

So, you just need to include Rackable in your class and implement methods for the the appropriate REST verbs. This means you can create a hello_world.ru file like this:

require ‘rackable’

class HelloWorld
include Rackable

def get()
“Hello, world!”
end

end

run HelloWorld.new

Read More

By Ric Roberts / July 17, 2009

Phillip TolandUntil recently, I had been using the Curb library for making HTTP requests with Ruby, and I must say I was fairly happy with it. Phillip Toland, on the other hand, wasn’t satisfied with Curb’s API and the fact that it is tricky to modify (being implemented mainly in C). So, he came up with an alternative called Patron, written as much as possible in Ruby.

Patron is based on libcurl, just like Curb, but it aims to provide a simpler interface whilst still taking advantage of libcurl’s strengths. To use Patron you can instantiate a Session object with your desired options:

sess = Patron::Session.new
sess.base_url = “http://search.twitter.com/”

…and then call methods against that session to perform your GET, HEAD, PUT, POST or DELETE operations. Read More

By Ric Roberts / July 17, 2009

sirenSiren is a JSON and JSONQuery interpreter for Ruby by James Coglan. Before I dive into talking about Siren, a little bit of background:

JSON is a lightweight, human-friendly data interchange format, for which there is already good Ruby support in the form of the json gem. JSONQuery was originally added as part of the dojo javascript toolkit and provides querying tools for JSON such as filtering and sorting etc. (check out this article on SitePen for more details). You can also find a stand-alone port of JSONQuery on Github.

Anyway, Siren lets you run queries against any type of Ruby object. Read More

By Ric Roberts / July 16, 2009

aliasA couple of weeks ago we featured Gabriel Horner‘s Hirb framework for formatting irb output. I’ve recently been playing with another of his projects, Alias, which further enhances the Ruby Console experience (but it conceivably could be used in your Ruby programs too).

It’s already possible to set up aliases in your .irbrc file, but this can get confusing and it’s easy to run into conflicts. Alias takes a more structured, hash-based approach which (by default) lets you set up aliases for constants, instance methods or class methods. For example:

create_aliases :instance_method, “ActiveRecord::Base”=>{“update_attribute”=>’ua’}

You can store and retrieve your aliases in an easy-to-read YAML configuration file and it’s simple to have script/console (or irb) load them automatically from that file on start-up, by adding a small amount of code to your environment.rb (or .irbrc) file. Read More

By Ric Roberts / July 13, 2009

gibblerInspired by Git (the version control system), Delano Mandelbaum has come up with a library called Gibbler, which produces hashes and history for Ruby objects.

Calling the gibbler method on an object will produce a 40 character git-like SHA1 hash (or you can use gibbler.short to get just the first 8 chars).

my_object.gibbler.short # => 4c558a56

Gibbler can also track changes to an object. Every time you call gibbler_commit, it will create a clone of the current object and store it in an instance variable. And just like with git, you can view or revert to any version in the history. Read More

By Ric Roberts / July 7, 2009

Why’s Markaby is a really convenient bit of Ruby for generating HTML in your applications, rather than having to fiddle about with string interpolation or ERb, tangling together HTML and Ruby.

However, Markaby can be slow compared with other options like Erubis or HAML. Magnus Holm has been working on an alternative solution called Parkaby, which uses ParseTree for improved performance (coming out up to 20 times faster than Markaby in Magnus’s tests).  Parkaby uses near-identical syntax to Markaby, so you still get to write beautiful ruby code for building your HTML.

Parkaby {
html {
head {
title “happy title”
}
body {
h1 “happy heading”
a “a link”, “href” => “url”
}
}
}

Magnus admits that Parkaby is currently only experimental, and that there’s room for more optimization, but I think he might be onto something. Read More

By Ric Roberts / July 6, 2009

When running a Ruby daemon which executes code in a loop, if the process is killed while something is happening then problems can occur if the code doesn’t handle all the exceptions properly or if the loop isn’t broken cleanly. RobustThread is a Ruby class by Jared Kuolt for the creation of threads, which helps to alleviate this problem.

RobustThread lets you set up loggers, exception handlers and callbacks for threads, exposing the actual thread via an attribute.  By default, it logs to the standard output, but this is configurable by using a Logger object.

You can apply labels to loops or threads, which can be useful for logging and debugging.  Read More

By Ric Roberts / July 2, 2009

anemone Anemone is a free, multi-threaded Ruby web spider framework from Chris Kite, which is useful for collecting information about websites. With Anemone you can write tasks to generate some interesting statistics on a site just by giving it the URL.

Its only dependency is Nokogiri (an HTML and XML parser). Other than that, you just need to install the gem to get started using Anemone’s simple syntax which, among other things, allows you to tell it which pages to include (based on regular expressions) or define callbacks.

This example taken from Anemone’s homepage prints out the URL of every page on a site:

require ‘anemone’

Anemone.crawl(“http://www.example.com/”) do |anemone|
anemone.on_every_page do |page|
puts page.url
end
end

The bin folder in the project contains some more in-depth examples, including tasks for counting the number of unique pages on a site, the number of pages at a certain depth in a site, or a list of urls encountered.  Read More

By Ric Roberts / June 30, 2009

mongo mapper MongoDB a is a high-performance, open source, schema-free, document-oriented database written in C++. It’s sort of a cross between scalable key/value stores and traditional functionality-rich relational databases.

MongoDB might be useful as a fast, simple, non-transactional data store for a web application, or as a caching mechanism. You don’t ever need to worry about migrations due to Mongo’s schema-less nature.

Getting started with MongoDB using Ruby is now fairly straightforward thanks to the Mongo Ruby driver. This provides access to the core Mongo database operations, and natively supports many Ruby objects without requiring conversion (including nested hashes). There’s even an ActiveRecord connection adapter for Mongo. Read More

By Ric Roberts / June 28, 2009

hirb The Interactive Ruby Shell (irb) and the Rails console are great for interacting and experimenting with your ruby application code, but sometimes it’s hard to visualize the output. Gabriel Horner has come to the rescue with Hirb: a ‘mini view framework’ for irb which is designed to improve the default output to make it more human-readable.

Hirb does this by formatting console output according to its type, and paging if there’s more than a screenful to display. For example, Hirb will automatically display ActiveRecord model instances in a non-wrapping, table-like view.

irb>> Tag.last
+—–+————————-+————-+
| id  | created_at              | description |
+—–+————————-+————-+
| 907 | 2009-03-06 21:10:41 UTC | blah        |
+—–+————————-+————-+
1 row in set

There’s also a helper provided which displays a collection of arrays or hashes as a tree. Read More