blog is moving

April 24, 2008

This blog is moving to I’m moving to blogger to have more control of pasting code snippets, etc. Follow me there!


I released a little gem this week that helps to deal with what can be an annoying issue in larger ruby projects: requires. Unfortunately, relative requires will always be relative to the file that launched the application, not to the file making the require itself. What a pain. This means that if you’re launching your application from rake, from a test suite or from the command line, you need to rely on some trick. These include unshifting your lib directory onto the load path or using the File.dirname(__FILE__) trick. Both are cumbersome and a bit annoying, but I think each has a far more detrimental side-effect: readability.

If I’ve unshifted lib onto the $PATH variable, I make all requires within my application (or gem, etc.) as though they are being made from the base of the lib directory. Ok, this gives us a convention, but it also gives us a lot of confusion. And, more importantly, we can not take any file, at any time, and run it! I want every ruby file I write to be self containing and explicitly define it’s dependencies. I want every ruby file I write to be able to be loaded into irb without consequences. Need is a gem that helps with these problems.

To use need, simply “need” a file using a relative path from the file that has the dependency. And that’s it. This will work no matter where you application is launched from. And your file can always be loaded directly into irb. And the dependencies are clear (as are their locations from the file itself) without knowing any extra application-level magic. To use need, just do:

require 'rubygems'; require 'need'

Need is implemented as an extension to Object and it uses the binding of the block (yes, those curly braces are needed in the call, no pun intended) to remember the scope of the file where need was called, and hence eval __FILE__ with that file’s location and not the location of the Object extension file itself.

What are you waiting for, install it now:

sudo gem install need

Drop me a line if you have any questions, and happy needing!

RMagick on Ubuntu

September 11, 2007

I had a hell of a time getting RMagick installed on my powerbook, but it was much less of a hassle on ubuntu. Here’s all I had to do:

sudo apt-get install imagemagick libmagick9-dev
sudo gem install rmagick

It worked on the first try! This was a nice surprise as it took me days of searching the web to get everything working on OSX. Let me know if you run across any problems with this.

Back in the US

September 11, 2007

Well, I’m back in the US after a great 2 1/2 weeks in Bangalore, and I’m awaiting my first assignment with ThoughtWorks. While on the beach, I’ve been toying around with an Open Source project we started with some colleagues while in India. Should be interesting to get some experience with distributed development first hand.

We’ve got Mingle up and running and hopefully we can get someone to throw cruisecontrol rb up on a box somewhere so we can learn how these tools help/don’t help our development efforts.

It looks like I won’t be able to stop myself from buying a new Macbook Pro either. I’ve run out of excuses as I’m able to use it for work now. Nothing much else exciting right now, but I’ll post if/when anything turns up.

drew.visited << “India”

August 19, 2007

Two slightly major pieces of news:

  1. I took a new job with Thoughworks as an application developer. This means I get to spend the majority of my time creating applications using ruby and java! And in an agile environment to boot! I’m extremely excited, my first day is this week.
  2. As a result of this career move, I’ll be going to India for two and a half weeks. I leave this week and I’m extremely excited. I’ll try to post a few pictures.

Expect the frequency of posts to increase in the near future as things have settled down on the job front. See you in the Garden City.

First, sorry for the lack of posting over the past month. Things have been fairly crazy/interesting on the job front. More on that later.

For now, I want to share a nifty little method I put together (I’m probably not the first one) to deal with a situation I recently encountered. I had a bunch of data in an array and I wanted to compare each element with the elements next to it, one at a time, and find any place where the difference between the numbers was more than 10. Here’s what I added to the Array class to accomplish this task:

Little-bitty code snippet

This allows me to perform the function I wanted simply by doing this:

my_array.adjacent_pairs.inject(0){|count,(i1,i2)| count + ((i1-i2).abs > 10 ? 1 : 0)}

Nice and easy. Ruby has made it a breeze for me the solve problems like this in simple, understandable ways without extra fluff.

It appears ruby is even smarter than I imagined; this functionality is already present!

require 'enumerable'
my_array.each_cons(2){|a,b| #do something with a and b }

Ruby has open classes. “Who cares?” you say. You should. Yes you! Open classes really simplify a lot of programming problems when you can learn to use them correctly. First and foremost, what does it mean to have open classes? Well, this means that you, the programmer, can crack open ANY class and add methods to your heart’s desire! And nothing is off limits. This means I can break into the Integer class, add a helpful method for my program, and sneak out again without anyone being the wiser.

So how would one use the the fiendishly clever practice of open classes in every day coding? Here’s an example of a problem I ran into while working on project euler problems:

I often found that I needed to know the number of digits in an Integer for solving these problems. Let’s say I want to get the sum of the number of digits in all the numbers 1..100. Here’s the old way:

Old Way

Now this is all fine and dandy and the code works great. However, there is a logical conundrum here. Why is num_digits just hanging out there by itself? We know that this method will always be called on Integers and, therefore, it makes much more sense to add a new instance method to the Integer class. Ruby open classes to the rescue!

Super fabulous open classes way!

Ahh, I feel so much better, don’t you? The only subtlety here is that we’re using self inside our num_digits method. This simply references the instance of the integer we are currently dealing with. So remember, next time you find yourself writing a method that would be better suited as an instance method of a class, break on in there! Ruby classes aren’t shy.