unbound imagination - a blog about programming

«

Capistrano, meet Twitter: Keep your users updated through Twitter

June 04, 2008

I recently deployed a beta of an application I'm working on and I thought it'd be nice to keep users updated about any new features or updates through twitter. So I wrote a spiffy Rails plugin that just adds a capistrano recipe for updating twitter statuses when you deploy.

capistrano_twitter just prompts you for a status update when you deploy and then updates the twitter status for the specified user (in config/deploy.rb). Of course, if you leave the status blank, no update will be made.

Check it out for yourself: http://github.com/arya/capistrano_twitter

Javascript Dependency Manager and Google AJAX Libraries

May 29, 2008

On Tuesday, Google released a new service where they host popular javascript libraries such as prototype and jQuery. This allows you to link to their copy of the libraries instead of your own so you can reduce the load on your server and speed up the client's experience.

They plan on archiving all stable versions of each library and allowing you to choose which you want to link to. They also provide compressed versions of all the libraries except scriptaculous and prototype (I'm not sure why only those two are left out).

Anyway, I've added support for using the Google copy of these libraries into my javascript dependency manager plugin. To use it, upgrade to the newest version of the plugin and create an initializer with the following code:

   1  JavascriptHelper::JavascriptHelperConfig.use_google_ajax_libs = true

Now when you include prototype, jQuery or any of the other supported libraries using the javascript dependency manager plugin, it will refer to the Google hosted file. By default the Google hosted libraries are not used, so users who want to stick to their own ways can continue doing so without any changes.

You can specify library versions and preference for compressed versions in the initializer as well, view the readme for details on how to do that.

Book Review: The Ruby Programming Language

May 19, 2008

This past month, I've had the opportunity to read through O'Reilly's new Ruby book, The Ruby Programming Language; thanks to OCRuby and O'Reilly for providing the book.

Now before I share my opinions on this book, let me give you a quick background about me and my programming history just so you have a better understanding of where I'm coming from. I'm a 21 year old college student studying Information & Computer Science at UCI. I've been programming for about 7 years now: first in PHP, then a bit of Java, and for the past 2 years Ruby. I've worked professional in Rails, done numerous Rails projects working for myself (the most recent which recently launched is UselessHypothetical.com), worked on school projects in Ruby (AI project class, operating systems project class), solved several project euler problems using Ruby, and of course written tons of small Ruby scripts as utilities and for fun. I feel I have a pretty good understanding of the Ruby language ranging from syntax to standard library to those 'gotchas' that most (dare I say all) languages have. Alright, enough with the boring stuff, time to get to the book.

For those of you that don't know: Ruby was created by Yukihiro Matsumoto (a.k.a "Matz"). Fortunately, Matz is also the co-author of this book, which is awesome because well, he probably knows the language better than anyone. I don't know it's because of Matz or if it's just coincidence, but this book is incredibly thorough. In the third chapter, "Datatypes and Objects", I learned quite a bit about the nitty gritty of Ruby's syntax and how some of the built-in classes work. For example, I'd seen this syntax a lot:

   1  %Q{This string is quoted and can have "quotes" without a problem}

But did you know that it doesn't have to be braces that surrounds the string?

   1  %Q^This string is quoted and can have "quotes" without a problem^

Yea, I didn't know that. Also, did you know that there is Ruby syntax for single character codes?

   1  ?a # => 97, the character code for 'a'

Anyway, there were tons of small things like that in this chapter that were new to me including fun facts about how the Strings, Ranges, Hashes, and Symbols work. One of the great things about how the book is structured is how it avoids bombarding you with out-of-context standard library methods and usages; each chapter/section gets to the point and stays on topic.

The later chapters get into more advanced topics such as lambdas, procs, and closures and of course the well-known metaprogramming aspect of Ruby. I didn't get a chance to read each and every paragraph yet but I've skimmed pretty much everything. It's got tons of info jam-packed into all 444 pages and does an amazing job of keeping it all organized and presenting it in a manner that can be easily understood.

Throughout the book, there are sections that include explanations about how something is changing from Ruby 1.8 to Ruby 1.9, which is helpful for anyone intending to switch to Ruby 1.9 when that is completed and becomes mainstream. Unfortunately, these sections aren't marked by any visual indicator, so skimming for them doesn't work too well.

In conclusion, this book is by far the best Ruby language book I've seen yet and extensively covers Ruby and it's mainly features. It's an easy read, organized into logical chapters, and is relatively short in comparison to the Pickaxe book (864 pages) without losing content.

I recommend this book to:

  1. experienced programmers looking to learn Ruby
  2. Ruby programmers who are interested in knowing every-little-thing about Ruby
  3. Ruby programmers who will be switching to Ruby 1.9

Update: The purpose of this post isn't to educate anyone on what this book contains or what it covers but simply to give you a quick insight into how I enjoyed it and how I found it useful.

ActiveRecord AssociationTypeMismatch: User expected, got User

May 13, 2008

Look familiar? ActiveRecord, fortunately, tells you when you try to assign an object of the wrong class to an association so you'd get this error if you tried to assign a String as the Comment for a Post (which is the correct usage). But sometimes you'll get the error in this case:

   1  @comment = @question.comments.build(params[:comment])
   2  @comment.user = current_user # raises "User expected, got User"

Now just earlier today, I got this "User expected, got User" error message and it stumped for me a few minutes. I then realized it's because I changed the User class, which usually isn't a problem since Rails reloads your models in the development environment. But since the Comment class is in a plugin (actsascommentable) and plugins aren't reloaded every request, the Comment class was expecting the User class as-it-was when mongrel was started (and the plugin was loaded).

Sooo.... to save developers between 5 seconds to 5 hours, I added this message to the exception raised (only if the two classes have the same name):

   1  (did you change the User class? try restarting mongrel)

I wrote a patch for the changes to add the helpful message. It's super-simple, but hey, I gotta start somewhere. I also made a ticket on the Rails LightHouse project, hopefully it'll get pulled into Rails.

Update: I found this guide on contributing to rails and see that I'm not supposed to fork Rails in GitHub unless its for something big. So I deleted my fork.