Ruby gymnastics

@strips = @user.comics.collect {|c|    
    c.strips.find(:all, :conditions => [“date > ?“, 7.days.ago])}.
    delete_if {|c| c == nil}.   
    sort_by {|c|}

The previous shows ruby in much of it’s glory, and is code that I needed this weekend for one of my side hacking projects.  I’ve colored it to match what xemacs shows me, just to make it a little more clear.

First off, it shows off the power of mixins. 7.days.ago does exactly what you would expect, providing you with a date object.

Second, it shows off the power of collect (aka map in many other languages).  Collect lets you iterate through a list you have, and return a new collection based on an arbitrary transform.  In this case returning a list of strip objects for each comic.

And lastly, it shows the fact that collection operations can be chained.  My list of lists becomes a single order list, I purge out nils (probably redundant at this point), and then sort all the objects by their date field.

Ruby is such a fun language to program in. 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s