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

By Ric Roberts / July 23, 2009

backgroundedLike Ryan Sonnek, I’ve dabbled with a few different ways to run background processes in my Ruby apps, even resorting to knocking together my own (far from perfect) solution. As Ryan says on his blog, many popular libraries have complicated interfaces and don’t “feel right”.

I tend to disagree, however, with his statement that every ruby background job solution sucks, and I’m sure he doesn’t really mean it. Although his offering, Backgrounded, is refreshingly simple and concise, it’s effectively just a wrapper for other solutions.

With Backgrounded, if you want a certain method to always run in the background, you can specify it like this:

class User
backgrounded :do_stuff

def do_stuff
# do all your work here
end
end

…and then just call that method in the normal way. Read More

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 Peter Cooper / July 15, 2009

rubyicons-1.gif

Several weeks ago, stock icon company Iconshock offered to design some icons for Ruby Inside to give to its readers. Initially skeptical, I was convinced to give it a try after seeing some of their other work. Now, the first 6 icons are back and.. they’re totally free for you to use. Grab this ZIP file that contains PNG and EPS (ideal for Illustrator) versions of the icons and give them a go!

The six icons signify Ruby on Rails, Ruby itself, two file types (Ruby and RubyGems), an application, and a generic cog. All can be used as you see fit. Read More

By Ari Brown / July 14, 2009

With people occasionally talking about “Code vs. Data”, it only makes sense that you should be able process over code as you would a string. Sexp Path is a code processing tool that allows you to search over and process Ruby code in the form of S-Expressions.

For those who don’t know, an S-Expression (or simply, a “sexp”) is an iterable way of representing code or data. Using Ryan Davis’ Parse Tree, you can parse Ruby files and process over them using Sexp Path. It’s a bit like like XPath or regular expressions for your code.

The foundation of Sexp Path is the query, formed with Q?{ … 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 Matt Sears / July 1, 2009

IntegrityIntegrity is a simple and lightweight Continuous Integration server written in Sinatra (a DSL for quickly creating web-applications in Ruby). When commits are pushed to a Git repository, Integrity builds, runs tests, and reports the build status to each team member. It supports a variety of notifiers including Email, IRC, and Twitter.

When it comes to developing large projects with multiple team members, it’s common nowadays to set up a Continuous Integration (CI) server. CI is a development practice where developers combine their work frequently and run tests over the whole project in order to identify errors early. Wikipedia has a good summary of the practice. 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

By Peter Cooper / June 22, 2009

The latest installment of the series of posts crammed with random Ruby links, articles, and resources to kick off your week!

gogaruco.png

17 High Quality Videos from GoGaRuCo

Earlier this month, the videos from the GoGaRuCo (Golden Gate Ruby Conference) conference that took place back in April went online. The talks are all available in MPEG 4 video and MP3 audio formats. Video and audio quality are really good overall (no annoying humming or reverb that often plague such undertakings).

Talks include Using Ruby to Fight AIDS, MacRuby and HotCocoa, Building Custom Web Proxies in Ruby, and Sinatra: The Framework Within, although there are 17 to check out overall. Read More

Recently Popular Posts