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

By Peter Cooper / June 12, 2006

A few days ago, Courtenay of Caboose posted an article called ‘pretty tables for ruby objects‘ that give a MySQL-command-line-client style textual view of data stored in your Rails database. The syntax worked like this:

puts Invoice.pretty_table(nil,{:max_width => 50, :line_spacer => ‘-’, :find => { :order => ‘created_at desc’ }})

For an output like so:

| created_at | updated_at | name | due_at | p |
—————————————————————————————————————
| Fri Jun 09 14:17:00 PDT 2006 | Fri Jun 09 18:34:55 PDT 2006 | Website | Sun Jul 09 14:17:00 PDT 2006 | 1 |
| Fri Jun 09 14:01:00 PDT 2006 | Fri Jun 09 18:34:55 PDT 2006 | May | Fri Jun 23 14:01:00 PDT 2006 | 1 |
| Sat Jun 03 12:25:00 PDT 2006 | Fri Jun 09 18:34:55 PDT 2006 | Invoice 3 | Mon Jul 03 12:25:00 PDT 2006 | 3 |

It didn’t make sense to me and I suggested developing a version that worked in this way instead:

Something.find(:all, :conditions => ‘whatever’).pretty_print

This way you can use your regular model accessors and simply use the ‘pretty’ output on any result set whatsoever. Read More

By Peter Cooper / June 12, 2006

BackgrounDRb, by Ezra Zygmuntowicz, is a system that lets you run long running tasks in the background, and not in the scope of your application. For example, you might have a Rails application that needs to send 100 e-mails, but rather than tie it to an HTTP request, you can simply pass off the job to a BackgrounDRb object and let the user know everything is proceeding as planned. There’s more information here.

It’s powerful stuff, and worth looking into if you have anything like mass mailing or other remote communications that isn’t request dependent, but still needs to be carried out. Read More

By Peter Cooper / June 12, 2006

Coda Hale is a Rails developer from Berkeley, CA, who’s developed a ton of useful plugins. I’ll leave the descriptions to Coda:

content_cache

Cache rendered views within the context of your layout, allowing you to mix dynamic content with cached static content.

dollars_and_cents

Stores prices as integers in your database, but access them as floats.

dynamic_session_exp

Allows you to configure Rails to serve up session cookies with expiration dates relative to the current time, instead of some fixed date in the future.

http_caching

Allows your Rails application to take advantage of the caching mechanisms built into HTTP 1.1 (i.e., 304 Not Modified return code). Read More

By Peter Cooper / June 12, 2006

Simplelog

SimpleLog is a ‘simple Ruby on Rails weblog application’ by Garrett Murray. It features tagging, search, multiple author support, pinging, provides an RSS feed, and it’s fast. It’s only one of several solutions available now, but the code is available for free, so there’s always something to learn :) Read More

By Peter Cooper / June 12, 2006

It’s not the nicest code you’ll ever see, but it’s a great demonstration of the power inherent in Ruby on Rails. Miklos Hollender demonstrates how he created a version of Web 2.0 app, Reddit, in under 20 minutes using Rails, scaffolding, and a little elbow grease. Source is included.

An interesting aside based off of this is an interesting video demonstrating how to recreate Reddit in Lisp in 20 minutes, as Lisp is somewhat a cultural uncle to Ruby, it’s worth watching. Read More

By Peter Cooper / June 11, 2006

Paul Lutus says:

I have always felt that a language hasn’t really arrived until there is a source beautifier utility for it.

So he developed one. It’ll take a pile of messy code, indent it, and make it look good. Enjoy! Read More

By Peter Cooper / June 11, 2006

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.

If playing with OpenGL, 3D graphics, and building some simple games sounds appealing to you, Rubygame is the place to be. Read More

By Peter Cooper / June 11, 2006

One of the features of Rails 1.1 was the ability to automatically detect clients that could understand XML and send XML responses to them using the respond_to method. This also works for AJAX requests and HTML, of course. Unfortunately, though, this relies on XML and API clients sending the correct Accept headers in most cases, and, as we all know, users aren’t necessarily so smart (even API users!).

In response to this problem, Edge Rails now accepts the format parameter on the URL, like so: http://www.yourapp.com/post/show/1?format=xml So while the Accept header method still works, you can also pass in the format directly via the URL. Read More

By Peter Cooper / June 9, 2006

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:

require ‘win32ole’
ie = WIN32OLE.new(‘InternetExplorer.Application’)
ie.navigate(“http://{internal-website}/your-desired-url”)
sleep 1 while ie.readyState() != 4
html = ie.document().documentElement.outerHTML
ie.quit()

Excellent! This makes me wonder if there’s a similar mechanism (not AppleScript) for OS X. Anyway, learn more, including more complex demonstrations, at Steve’s post. Read More

By Peter Cooper / June 9, 2006

The Charming Ruby Compiler is charming not just by name, but by nature. It’s a thesis project at the Computer Science Department at Chalmers University of Technology in Sweden to develop a proof of concept Ruby compiler that compiles to the C– intermediate language. So far, they’ve only got the basics working, but it’s an interesting project, if only to learn about C–, an interesting ‘portable assembly language’ that makes life easier for compiler developers. It seems a little like Parrot, and is funded by Microsoft Research and the National Science Foundation. Read More

By Peter Cooper / June 9, 2006

Rather than use the send_file or send_data methods to send an entire, completed bulk of information back to the client, David N. Welton wanted to, effectively, print stuff to the client bit by bit. He worked out how to do it and presents his findings and sample code here.

A preview of his ‘output data to CSV line by line’ method: Read More

def generate_file
@headers["Content-Type"] = "text/comma-separated-values;"
@headers["Content-Disposition"] = "filename=\"some.file.txt\";"

i = 0

render :text => Proc.new { |response, output|

output.write("some generated text…")

}, :layout => false
end

By Peter Cooper / June 9, 2006

Chris Anderson looks at deploying Rails apps with Capistrano and Mongrel on Planet Argon. He provides a whole pile of recipe snippets, as well as some Capistrano recipes for Mongrel ‘spinner’ and ‘restart’ tasks.

If all of this sounds like double dutch, you might want to skip this post, or learn more about Capistrano and Mongrel, as they’re both very useful tools when it comes to Ruby on Rails. Read More

By Peter Cooper / June 9, 2006

I’m not a Windows user at all, but RForward struck me as perhaps being very interesting to those wanting to roll out Rails apps on Windows servers. It sounds like some of the new UNIX-side solutions to deploying Rails applications.. that is, proxying requests from one daemon to another rather than doing it all from one.

RForward is a reverse proxy designed to make Ruby on Rails applications easy to install into Microsoft’s IIS web-server.

The traditional way of installing Ruby on Rails applications into IIS is to use FastCGI. I have written a tutorial – as you can see it is quite long-winded and error-prone. Read More

By Peter Cooper / June 9, 2006

maiha from #caboose ran some benchmarks and produced this graph of ‘requests per second’ for a basic app under different dispatcher setups (Apache, lighttpd, Mongrel, etc.):

Rails-Deployer-Benchmarks

It shouldn’t be taken as definitive (the Litespeed + lighttpd time seems suspicious to me), but it definitely demonstrates the power of lighttpd and Mongrel. Read More

By Peter Cooper / June 8, 2006

Lately I’ve been finding little known Ruby gems and trying them out. My latest find is EventMachine. EventMachine describes itself:

Ruby/EventMachine is a fast, simple event-processing library for Ruby programs. It lets you write network clients and servers without handling sockets- all you do is send and receive data. Single-threaded socket engine- scalable and FAST!

Simply install with gem install eventmachine and you can create a fast, multi-user server / daemon like so:

require ‘rubygems’
require ‘eventmachine’

module EchoServer
def receive_data(data)
send_data “>>> You sent: #{data}”
close_connection if data =~ /quit|exit/i
end
end

EventMachine::run {
EventMachine::start_server “127.0.0.1″, 8081, EchoServer
}

Once you run this, you can telnet to localhost on port 8081 and have a conversation with yourself! Read More

Recently Popular Posts