July 2006 Archives

Rob Orsini

Treemap on Rails

AddThis Social Bookmark Button

You may have used the Active Record “acts_as” extensions that ship with Rails, such as acts_as_list, or those added by third-party plugins, such as acts_as_attachment. In this post I’m going to cover how to use a new plugin for Rails by Andrew Bruno, called acts_as_treemap.

What is a Treemap?

A Treemap is a diagram that allows you to easily visualize hierarchical information, or trees. The first treemap was used to visualize the directory structure of a filesystem; to make it very easy to identify the disk-hogs (files taking up a disproportionate amount of disk space) on a system with very limited resources. A more recent example of a treemap is one that Tim O’Reilly has posted on O’Reilly’s Radar that shows recent trends in programming language book sales.

The following treemap show two dimensions of information: Square size represents sales volume, and color represents rate of growth.


Book Sales

In order to represend your data with a treemap, your data must be modeled as a tree. The tree data model is much like that of an XML document, where there’s a root or parent node, and zero or more child nodes. Each subsequent node may have it’s own children, and so on.

In SQL, this structure is simple to set up. You create a field that serves a the primary key (in Rails that’s always “id”), and another field that stores the parent id of each record (e.g. “parent_id”). Note that the root node will have a parent_id of “NULL.” There can be more fields, of course, but these are all that are required to structure records as a tree.

The following diagram shows the relationship between data in a table (A), a tree structure (B), and a treemap(c).


Tree Structure

I’ve posted a full how-to for Andrew’s acts_as_treemap Rails plugin over at Tupleshop.com. Please feel free to experiment with it and report back about your experience. Please visit: Using acts_as_treemap with Rails

pat eyler

AddThis Social Bookmark Button

FOSCon was amazing! Picture 100 Ruby geeks and Ruby geek wannabes gathered in a fairly small industrial building that’s been converted into something I heard described as “a frat house for geeks”. There were five talks (Lucas Carlson, Topher Cyll, Ryan Davis>, Geoffrey Grossenbach, Jim Weirich, and Amy Hoy) and a great deal of side room discussions. As good as OSCon is, FOSCon is even better, albeit in an anarchic sort of way.

The pdx.rb started FOSCon last year out of a desire to particpate in OSCon without needing to pay the conference fees. They invited a bunch of OSCon speakers and a couple of other folks to come out and present to an underground audience. This year, O’Reilly invited them into the fold, and FOSCon became a track unto itself — an all Ruby all the time track that came with pizza, drinks, and attitude.

So, other than making you jealous why am I writing all of his? Simple. I want you to be able to enjoy the same kind of experience — and you won’t have to come to Portland to get it. How’s that you say? Again, simple. All you need to do is work with you local Ruby Brigade (and maybe others in the region if you’re lucky enough to have them) and plan a regional Ruby Conference (and here and here). Pick a Saturday, find a location, invite a speaker or two from somewhere else, and put out a call for papers in your area. It’ll be great. Trust me.

Steve Mallett

AddThis Social Bookmark Button

Over the weekend I saw that O’Reilly’s Ruby Cookbook edition has become available. I’m a big fan of the cookbook/recipies format so mine should be on the way Monday morning. Poor delivery person - it’s 906 pages. Wow.

Curt Hibbs

AddThis Social Bookmark Button

This really makes my day. JetBrains, the makers of the fabulous IntelliJ IDEA, are adding support (via a plugin) for Ruby and Ruby on Rails.

It was just a very brief mention at the very end of on hour long video presentation of Team Server and IntelliJ 6.

You can watch the video here. If you only want to see the Ruby/Rails part, then fast forward to about 1 hour and 12 minutes into the video.

via Richard McMahon.

Geoffrey Grosenbach

AddThis Social Bookmark Button

How much memory do you need to run a Rails app?

A few months ago I moved my Typo-based blog to a Virtual Private Server. A VPS is a server that is shared with a few other sites. You are guaranteed a certain amount of RAM and have root access to the box. I followed Ezra’s instructions and easily installed MySQL, Lighttpd, FastCGI, and Rails. Finally, I removed Apache since I wouldn’t be using it.

I didn’t do much tuning. MySQL is a basic install and I have one Rails FCGI process and one PHP-FCGI process running (my site stats application runs on PHP). The site gets about 1,500 to 2,000 page views a day, so it’s not a high activity site. Still, it has been much more reliable and I have had only one hour of downtime in the last two months when the server rebooted and my reboot script was incorrectly configured.

A few weeks after signing up, I was notified that the hosting company had made a mistake and would be moving my site to a new server. In exchange for the inconvenience, I received a free upgrade from 128MB to 192MB of RAM. The upgrade revealed a few facts about the memory usage of a Rails stack.

pat eyler

AddThis Social Bookmark Button

Alternative Ruby implementations seem to be on the move throughout the Ruby community. JRuby is the furthest along at this point, so I decided to talk to Charles Nutter and Thomas Enebo, two of the principal programmers on the project. Read on to hear what they have to say about Ruby, JRuby, and the art of re-implementing Ruby.

AddThis Social Bookmark Button

About six months ago, I announced that I would be attending a Pragmatic Rails Studio in Chicago. Now, I’m not a hard core rails user and I am certainly no web designer. But we use Rails for a number of our internal sites and I wanted to get a little more firm footing with it.

I went into the studio already knowing a lot about the basics. Thus, I was a little worried that after the 3 days I might not take away much. Boy was I wrong.

Certainly, I learned a lot of Rails tricks from Mike and Dave. But there was also a lot of value in other things I learned, from talking with people who were using Rails. From seeing other people and their development environments. From discussing deployment strategies and where certain code should go (does it go in the model, the controller, the view, or a helper?). This is the stuff that you can’t put a price on.

One feedback item I (and I’m sure others) gave was that I would really like to see an offering for just Ruby. See, I’m much more of a Ruby guy than a Rails guy. I use Ruby all of the time, and more so than just for quick scripting. We have five $100,000+ motor controllers at our facility running on Ruby scripts. There are a number of backend scripts we have that keep this place going 24/7 all utilizing the power of Ruby. Simply put, Ruby is very important to me and to our company.

So, I’m sure you understand that I’m ecstatic to see that there is now going to be a Pragmatic Studio devoted solely to Ruby. The first (of hopefully multiple) such studios will be help in September in Boston and taught by Justin Gehtland and Stu Halloway of Relevance. What’s neat about this is that it’s not just a sitdown and learn Ruby type of show, it’s a “how can I use Ruby at work to get things done”. Think of things like LDAP, XML, and Domain Specific Langauges. Think of some of the things we take for granted in the Ruby community, like Rake and built in unit testing. This is where to learn it.

The thing about this is, even if you’ve been programming Ruby for a (relatively) long time, there’s most likely still something you’ll take away from this. Obviously, if you got into Ruby from Rails and want to learn more, this will be very good for you. But even if you’re a self-professed Ruby expert there’s still always something to pick up. It seems like a new Ruby library/binding/toolkit comes along almost everyday. Now you’ve got a great way to make sure you’re keeping up.

Now, I just need to ensure that they teach a short lesson on QtRuby.

* A quick note for the weary: If this sounded like an advertisement, well, it sort of was. I was very happy with my previous Studio experience and I wanted to share and make sure folks out there knew this was available in case they were interested. Please note that I received no sort of compensation related to this in return for writing it.

Gregory Brown

AddThis Social Bookmark Button

When I first found the inject method, it became a new favorite tool.

It was great to be able to turn something like this:


a = [7,8,9]
b = [1,2,3,4]
b.each { |e| a << e + 1}

into something like this:


b = [1,2,3,4]
a = b.inject([7,8,9]) { |s,e| s << e + 1 }

It was cool because it was even possible to build hashes this way, if you used a little trick.


b = [1,2,3,4]
a = b.inject({}) { |s,e| s[e.to_s] = e; s }

This should have given me the red flag though. Why should I need to pass the hash as the last value in the block?

go ahead, try something like this:

b = [1,2,3,4]
a = b.inject([]) { |s,e| s << e if e < 3 }

Now what I would *want* this to do is give me something like a #=> [1,2], but instead it gives us a #=> nil

So we have to do that annoying trick again:

b = [1,2,3,4]
a = b.inject([]) { |s,e| s << e if e < 3; s }

Now, I'm no nuby, but I suppose there is a little bit of nubyism in all of us. I just learned last week why inject was surprising me so much. It's not really just a shortcut for each that gives you a base value to build up, it's a functional method.

For those who aren't familiar with functional programming, there is alway the wikipedia, but the relevant part for this particular problem is that inject isn't designed to do anything destructive. That means things like << and += are bad and things like + are good.

That’s why you see the typical sum example of inject using just a + operator


sum = [1,2,3,4].inject(0) { |s,e| s + e } #=> 10

And if you want to build a hash, you can do so without the hack I showed before

hash = [1,2,3,4].inject({}) { |s,e| s.merge( { e.to_s => e } ) }

The reason these bits of code work is because the return value of the block is what becomes the new s, NOT the original parameter you passed to inject.

Now you might say ‘hey, this is probably pretty slow, building all these new references and passing them around’. I sort of thought the same thing my self, and am not quite sure how I feel about that.

But the idea is, you’re really fighting the function when you use destructive methods. If you find yourself needing to modify the original object rather than build a result set functionally, each isn’t THAT ugly. :)

Rob Orsini

AddThis Social Bookmark Button

Related link: http://blog.tupleshop.com/articles/2006/07/08/deploying-rails-with-pound-in-fron…

It seems that the Rails deployment dilemma is finally getting the care that it desperately needed to make the whole situation less of a pain in the neck. For a while there, everyone was hanging on the edge or their seats, hoping that Apache developers would fix Apache’s FastCGI interface that had fallen out of maintainence. While waiting for that, many people flocked to Lighttpd as a promising faster/lighter alternative to Apache that seemed to have its FastCGI interface under control.

Meanwhile, development of an alternative to WEBrick was under way, by a guy named Zed Shaw, called Mongrel. It seems Zed just got fed up and decided to change the Rails deployment world with his own bare hands. This is good news for all of us and the best thing about Zed is how much he cares about getting a situation together that works for everyone. (Also, if you ever need help with Mongrel, Zed is always right there with the answer.) So, this seemingly simple little pure HTTP web server has turned out to be much more usefull than anticipated. With the introduction of the mongrel_cluster gem, serving Rails applications with a small pack of Mongrel processes and a load balancer is a snap.

Software load balancers that people are using include Pen, Pound, and Apache2’s mod_proxy_balancer. Recently, on the main Rails blog, there was a post about setting up lighttpd with a single proxy to Pound which in turn served up a cluster of Mongrel processes. In reading the post and its comments I realized there seems to be some confusion about where Pound can exist within a typical deployment setup. A few people commented that with Lighttpd in front of Pound, the value of request.remote_ip was 127.0.0.1 (localhost) or something other then the IP of each external request.

There is no reason that Pound can’t sit out in front of Lighttpd, a pack of Mongrels, or any other web servers waiting to process and respond to requests. Because of the way Pound handles headers, the correct value of request.remote_ip is preserved by the time the request is received by Rails. In any case, the Pound docs send the vibe that the intention is to have Pound in front of other servers. Here’s a bit from the latest Pound README that talks about what Pound is and how it can be used:

Pound-2.0.9/README

  1. a reverse-proxy: it passes requests from client browsers to one or more back-end servers.
  2. a load balancer: it will distribute the requests from the client browsers among several back-end servers, while keeping session information.
  3. an SSL wrapper: Pound will decrypt HTTPS requests from client browsers and pass them as plain HTTP to the back-end servers.
  4. an HTTP/HTTPS sanitizer: Pound will verify requests for correctness and accept only well-formed ones.
  5. a fail over-server: should a back-end server fail, Pound will take note of the fact and stop passing requests to it until it recovers.
  6. a request redirector: requests may be distributed among servers according to the requested URL.

It’s number six above that give Pound its flexibility in terms of serving different requests to different back-end web servers. So, on with a simple demo of a Pound setup that passes requests back to a cluster of Mongrels, an Apache server, and a Lighttpd server.

Check out my full demonstration of how to set up Pound in front of your other servers, including a cluster of Mongrel processes serving Rails, here:

http://blog.tupleshop.com/2006/7/8/deploying-rails-with-pound-in-front-of-mongrel-lighttpd-and-apache

More of a visual person? Yeah, me too. So, here’s a little graphical teaser of the setup this article walks you through:

Derek Sivers

AddThis Social Bookmark Button

CD Baby is hiring a few top Rails programmers, anywhere in the world. Extended version of this entry has details, if interested.

Curt Hibbs

AddThis Social Bookmark Button

Videos of the RailsConf 2006 Keynote talks are becoming available for your viewing pleasure!

They were all excellent and well worth watching. So far, the keynote videos of Dave Thomas and Martin Fowler are available now, and the rest will be available soon.

AddThis Social Bookmark Button

VanRuby, the Vancouver Ruby Association went on a hiatis for some time to host the Canada on Rails conference, and is back at it. We are very pleased to announce the first free mini-workshop, titled.. Getting “really” started with Rails.

The event will be on Wednesday, July 26th, with doors opening at 6:30pm. This workshop will be an hour and a half long, and go over the lions share of the pieces you need to build your next web application with Rails. Including some advanced tactics that are key to saving time, and being a more efficient and effective solutions provider.

For those in organizations that have not yet made the decision to adpot Rails, this would be an excellent time to get in touch with the local developers including myself who are successfully using it to leverage their time and hear a few reflections of the journey.

Check out the full details and hopefully I’ll see you there!