InquiryLabs

Politics, Programming and Possibilities

Archive for the ‘Software Engineering’ Category

All Hail Rubinius! Rails is Running!

Some exciting news in the Ruby community has been reverberating today… Rails works in Rubinius!

Congrats to all involved, and thanks to EngineYard for sponsoring the project as well.

A Cleaner, Happier Blog

The dubious links have finally been cleaned up from my blog’s feeds. A search reveals none of the key words (e.g. loans, ringtones, porn) that were there before, so all evidence points to things being back to normal.

For the record, I tried to clean things up manually at first, but that quickly became an awful chore: more then 200 posts had been magically “edited” and the nefarious links inserted at the end. So I wrote a script to clean things up, using Ruby and Sequel:

require 'rubygems'
require 'mysql'
require 'sequel'

db = Sequel.mysql 'inquirylabs_wordpress',
  :user => 'user',
  :password => 'password',
  :host => 'localhost'

posts = db[:wp_posts]
posts.all.each do |p|
  posts.filter(:ID => p[:ID]).update(
    :post_content => p[:post_content].gsub(
      /(<font|<div\sid=wp_internal).*
        (porn|
         payday[-\s]loan|
         home[-\s]loan|
         ringtone).*$/x, "")
end

Much better. :)

Thanks to those of you who helped me to identify the problem and, recently, to see that I hadn’t scoured all of the dark corners of the website.

TextMate Footnotes 3.0

José Valim has improved the Footnotes plugin and kindly taken the lead on maintaining it for Rails 2.0. Read about his awesome new features here. Thanks, José!

Installing Merb in Your Home Directory

I have a shared account on a server where Ruby 1.8.5 is installed but Merb is not. To install gems, I added “$HOME/lib/ruby/gems/1.8/bin” to my path, and the following 3 lines to my .profile:

export GEM_HOME=$HOME/lib/ruby/gems/1.8
export RUBYLIB=$HOME/lib/ruby:$HOME/lib/site_ruby/1.8
export RB_USER_INSTALL=true

I had to add the last line because hpricot is a compiled extension that uses the “install” command to put it in the correct directory once it’s done. Tip came from this site. Without RB_USER_INSTALL set to true, I received the following error:

/usr/bin/install -c -o root -g wheel -m 0755 hpricot_scan.so
install: /.../hpricot_scan.so: chown/chgrp:
Operation not permitted *** Error code 71

Next, I installed rubygems:

wget http://rubyforge.org/frs/download.php/20989/\
  rubygems-0.9.4.tgz
tar xzvf rubygems-0.9.4.tgz
cd rubygems-0.9.4

ruby setup.rb all --prefix=$HOME --siterubyver=$HOME\
  /lib/site_ruby/1.8

And last but not least:

gem install -y merb

Thanks to Ian Mckellar for starting me off.

Update: For the record, the following gems were needed for my project:

  • mongrel (installs fastthread, daemons, gem_plugin, cgi_multipart_eof_fix)
  • sqlite3-ruby
  • ruby2ruby (installs ParseTree, RubyInline, hoe, rubyforge
  • merb_sequel (installs sequel, sequel_core, metaid, sequel_model, assistance)

What Makes jQuery a Good Choice?

I’ve been told that jQuery has been around since January of 2006. At some point during the last year, many developers within the Ruby community began to embrace it as an alternative to Prototype and Scriptaculous. Many of the programmer friends I look up to in the Merb community, specifically, are big advocates of jQuery.

So this post is an attempt at summarizing the strengths I’ve found in jQuery as opposed to the two other javascript libraries that I’ve worked closely with: mooTools and Prototype / Scriptaculous. My motivation for this is a soon-to-be-made decision regarding the javascript library that will be used in a browser-based application that I’m working on at a company in Chicago. The application needs a simple user interface and fairly powerful front-end. I’ve advocated the use of jQuery, and thought it would be useful to set out the reasons for my preference here on the InquiryLabs blog.

jQuery is well-documented

There is a fine set of documentation at jquery.com. Scriptaculous hasn’t seen much improvement in its documentation in over a year. Prototype’s documentation is (in my opinion) pretty, but still lacking.

jQuery is concise

It uses as its central paradigm the idea of “querying” the DOM for the desired elements and returning a collection (like a dataset, but with DOM elements) and then acting on the result, or set of elements. The reason it can be concise is that the element sets are chainable. For example:

document.ready(function() {
  for (var i = 0; i <= 6; i++)
    $('h' + i + ' [@id]').each(function() {
      $('<a class="anchorlink">\u00B6</a>').
        attr('href', '#' + this.id).
        attr('title', 'Permalink to this headline').
        appendTo(this);
    });
});

(Hat tip, lucumr) In the above example, anchor tags are added to all headlines in the document. Note the clever use of chaining which reduces the size of the code, yet retains its expressiveness.

jQuery is easily extended

Whether it’s a simple function or an entire plugin, jQuery is built for extending. The ability to query for “element sets” and then act directly on those results is not limited by the core code. Since it’s so easy to add custom functions to element sets, it becomes natural. For example, I found that I often needed to add or subtract a value from a CSS property. I solved this problem by extending the jQuery core element set, like so:

jQuery.fn.cssDelta = function(property, delta) {
  var value = parseInt(this.css(property));
  return this.css(property, (value + delta) + "px");
};

Using the above code, I can query for a set of elements and adjust the height property (for example) in one fell swoop. This will now add 5 pixels to the height of each “rack-item” element:

$('.rack-item').cssDelta('height', 5);

jQuery is small

According to Jeff Robbins and John Resig, the Drupal team chose it over Prototype because it is a mere 15k compared to Prototype’s 80k. The Dojo library is even larger.

Update:I decided to test this out myself to see what results I’d get. Resig’s comment was probably from jQuery’s earlier days when it was a micro framework. As far as I understand, it’s still possible to compress the jQuery library down to 18k or so, but that’s possible for Prototype, too. In fact, my tests show that un-gzipped, compressed code gets both down to 51k using Dojo’s ShrinkSafe online app.

Update 2:Thanks to Peter Higgins below for commenting on the size of Dojo. For comparison, its compressed (but un-gzipped) size is 77k.

jQuery has broad acceptance

Feedburner, Technorati, Drupal, Edgewall Trac, etc.

Contrasting Prototype

Here are some examples of the concise nature of jQuery, taken from the jQuery blog:

// AJAX Updater in Prototype:

new Ajax.Updater('placeholder', url, { method: 'get', parameters: par });

// AJAX Updater In jQuery:

$('#placeholder').load(url + par);

// Note: This example doesn’t deal with the obvious
// iteration benefits we get if we wanted to load the
// response into every <p> object, for instance.

// Adding a class to an element in Prototype:

Element.addClassName('element', 'className');

// Adding a class to an element in jQuery:

$('#element').addClass('className');

// Adding a class to a group of elements in Prototype:

$$('.element').each(function(node) {
  Element.addClassName(node, 'className');
}

// Adding a class to a group of elements in jQuery:

$('.element').addClass('className');

“That last one is the clearest example of the difference in methodology. Because jQuery is passing messages to jQuery objects, the code is barely changed. jQuery doesn’t care that we’re now adding a class to a group of objects instead of one object; the underlying code is the same (add the class to the set of elements in the object). Prototype, on the other hand, requires an iterator.”

“And as your code becomes more complex, jQuery’s scales easily, while nested loops become the norm in frameworks like Prototype.”

Notes on the Facets Gem

The Facets gem is fairly popular, and for good reason. I was poking through the documentation on the plane home from Chicago (I have a contract position there for the next month) and found a couple of neat things to share:

Facets library

  • Autoarray class—an Array that autovivifies any number of dimensions. For example:a = Autoarray.new
    a[3][4][5] = 1
    would create a 3-dimensional array with values initialized to ‘nil’ and the appropriate cell set to 1.
  • Dictionary class—a Hash that retains order. The order, by default, is whatever order its contents were given in; however, it can also enforce any arbitrary ordering that you provide.
  • Enumerable extensions—a (very nice) module that extends the Enumerable mixin. It’s huge! There are so many idioms captured in this that I can’t explain them all. But if you like Ruby for it’s concise syntax and easy chaining of things like ‘each’, ‘map’ and ’select’, you’ll like what you see in here.
  • File.rewrite—open, modify, write back to disk, all in a single ruby block.
  • FileUtils.head and FileUtils.tail—handy functions that let you get at the first few or last few lines of a file.

There’s plenty more to look at in the Facets gem… these are just a few that caught my attention. Install the gem and then use “gem server” to check out the documentation ;)

Did You Know About “gem server”?

I can’t believe I didn’t know about this until recently… it’s such a gem of a tip. :)

If you type “gem server” in the shell, you’ll start a webrick server that’s listening on port 8808 to dish out all of the help files for your installed ruby gems. Here’s a little alias I made that helps me get right to the documentation from the console:


alias gemdocs="gem server >/dev/null 2>&1 & \
    sleep 0.2 ; open http://localhost:8808"

Basically, it starts the gem server and ignores the log output. Next, it waits a fraction of a second for the server to start listening on port 8808, and then uses the Mac OS “open” command to open the default browser and points it to the gem server so you can start looking at your documentation.

Having a gem server is Brilliant!

Merb ActiveAdmin

Introduction

Merb ActiveAdmin is a drop-in backend for Merb applications that use the Sequel ORM. It gives application administrators direct access to specific models so they can add, edit or delete anything they need to. It also provides a way to add or remove associated data, e.g. one-to-many, or many-to-many relationships between models. These plugin features are provided with very low configuration so that setting it up is as easy as possible.

Watch the Screencast

The screencast is available here on a separate page.

Links

Download and installation instructions can be found at the github repository. See the README.markdown file.

The git repository is hosted at my account on github.

Final Words

Let me know what you think. I’m curious to know, specifically, how many people use Sequel as their ORM of choice. DataMapper and Stone have also been proposed as possible ORMs to support. And of course, ActiveRecord would be a nice choice too :)

I hope you enjoy it!

Github: The Killer App of Open Source Software

What do you get when you mix ambient findability, distributed version control, and social networking? The killer app of the open source movement: github.com.

When I first heard of github through the Merb development crowd, I wasn’t very impressed at first. To me, subversion was such a huge improvement over CVS that I was still quite happy to develop with the simplicity of a single-repository version control system. What’s more, I had tried SVK when it was “cool” and I didn’t really get much out of the added complexity—sure it was nice to be able to copy someone else’s subversion repo, and then modify your own fork without losing the ability to grab new changes from the origin… but there were a half dozen more commands to learn, and software to set up, etc., etc.

So why is git different? Well, first of all, it was designed by Linus Torvalds (the creator of Linux) and it is fast. The speed boost is enough to make me wonder how I could be so complacent about subversion’s performance—I just didn’t know any better. But speed is not the “killer” feature of git: it’s the distributed paradigm made possible by the low forking/merging barrier. When forking a project is as easy as naming the fork, and merging is as easy as a single command, the ability to do so and the desire to do so meet. Anyone can do it, as often as they want, and with whatever code base they want.

Now add social networking and you get an amazing thing: developers can see what other similarly-minded developers are interested in, and watch the growth of pet projects or interesting software. For example, I just found out about an innovative javascript database called jquery-database by browsing what other developers are interested in, and the gitnub tool for Mac OS X by exploring the Popular Watched page. This isn’t a vote-it-up, vote-it-down page—this actually reflects what people are watching on their aggregated feed page, or what code bases they are really forking to work on.

Speaking of forking, contributing may now become trivial too—just fork the project and you can add whatever features you want even without the original author’s permission. If your features stand the test of time, and if they are interesting to the maintainers of the original project, they can merge your changes back in. It’s like automating the process of diff and patch, to an infinite degree, while maintaining the ability to sync with the original code base as you play with stuff.

After getting started on github, I feel like I have shed a little darkness from an era when I was coding without light. For example, I’ve often thought, “Maybe I’m building something that has already been done… but even if my code is a duplicate of someone else’s project, is it worth it to try to get involved with them on the mailing lists?” Or, how likely is it that I will get commit access? It’s possible, of course, but it might be an investment in time that will prove fruitless.

Now, however, searching for other projects on github is so easy that I feel like everyone is helping me. We’re a world-wide network of developers building interesting things. Everything we write,
everything we watch, and everything we search can be shared. It’s all at our fingertips.

As you can tell, I’m excited to be a part of this new network. Github is now open to all for free or paid access, so come on aboard!

Faster Scheme

The Scheme package we use in class (DrScheme) is based on MzScheme, which is a popular and fairly standard version of Scheme. But it’s also very slow. According to these benchmarks, it’s 10 times slower than C on average, and 5 times slower than (Steel Bear) Common Lisp!

Of course, that’s still loads faster than Ruby, but it bothered me that something as similar to Lisp (and requiring the same amount of brain-wrangling for its parentheses) would not similarly share its speed. Well, it turns out that there several variants of scheme compilers that outperform MzScheme. Here are two interesting benchmarks (Twobit, Gambit) that put Chez Scheme (commercial) and Gambit Scheme (open source) on top.

Gambit Scheme claims to be as fast as C in some cases, which is a pretty ambitious claim. I’ll be checking it out soon.