The Rat Race! Ruby on Rails and AJAX on Amphetamines
Sterling Camden
This little Ruby on Rails application demonstrates some of the capabilities of AJAX with Rails.
The Game
Ten rats, chosen at random from a database, race against each other along a laned track to reach the cheese. Sometimes they move backwards, but ultimately one of them wins, another one places, and a third one shows. Their movements are controlled by a random number generator, biased according to their race history. That is, rats that have done well in the past are favored, which is reflected in their displayed odds. The race is updated once a second (or thereabouts), via XmlHTTPRequest (aka AJAX) in the standard Rails fashion using ActionView::Helpers::JavaScriptHelper#periodically_call_remote.
Quick Start
First, install Ruby and Rails. Unzip the download into a directory. Load the “ratrace” database from ratrace.sql. The database.yml file in the rats\config directory is setup to use MySQL, username “root”, no password. Make any changes you need to that file. cd into the “rats” directory, and startup the WEBrick server which is included by Rails. On Windows, this would be accomplished with the command:
ruby script\server
Now bring up your favorite browser and navigate to http://localhost:3000/rats/run. Make sure that if you have a firewall enabled, you are not blocking local access to port 3000.
Performance
Even though updating the race only once per second gives the game’s animation a distinctly “freeze-frame” flavor, it still seems to be a bit too quick for a browser-based applicaton. To see this, start the race. Let the rats get going a bit, then try clicking on the “Rats” link near the top of the page. On my system (1.8 Ghz P4 running XP SP2) using a local WEBrick server, I get the following very unscientific results with different browsers:
| Browser | Version | Time to load Rats:list page |
| Maxthon | 1.5.2 | 22 seconds |
| Internet Explorer | 6.0.2900.2180 | 16 seconds |
| Opera | 8.51 build 7712 | 13 seconds |
| Opera | 9.00 build 8501 | 13 seconds |
| Firefox | 1.5.0.4 | 5 seconds |
I haven’t tried the IE 7 beta yet. By viewing the WEBrick server’s output, I can see that Maxthon and IE6 have queued up many of the “update_race” calls, and these continue to be invoked even after successfully navigating to the Rats:list page. If you let the race run long enough, you can get so many of these queued up that you can’t ever get to the Rats:list page, and even after you close the browser the calls continue to come into the server. Several extra “update_race” calls occur with Opera, and only a few in Firefox. Thus it would seem that Firefox either manages to more quickly terminate the timer on navigation away from the page, or else it allows fewer calls to become queued. I don’t think it is processing the calls any faster, because the race appears to run at about the same pace in all of the browsers — although that is difficult to benchmark, given the random nature of the application.
This game has a long and fun history for me beginning in the 1970′s. See my blog on that here.
Posted in AJAX, Rails, Ruby, Web, Wildly popular |
2 Comments » RSS 2.0 | Sphere it!





[...] Ruby Brazil features my Rats on Rails. Obrigado! [...]
[...] been almost four years since I released the rats upon the unsuspecting world of Ruby on Rails. Since then, I’ve learned a lot about writing web applications – so I’ve been meaning to [...]