Feeds:
Posts
Comments

Archive for August, 2010

Bundler Without Rails

Yesterday I reached into a project I had not touched in months. When I wrote that Ruby script, it was supposed to be a one-off effort, but, as it usually goes for things like these, it had ended sticking around for much longer than anticipated.

I have RVM installed and I had installed many Rubies and done all kinds of gem manipulations. In short, the “environment” in which that project had worked was gone.

I had the “require” statements to guide me:


require rubygems
require dm-core
require dm-timestamps

require json

However, that’s not the whole story. In this specific case, DataMapper requires
more gems based on the connection string you give it.

I think we have all tried this:

  1. try to run a script
  2. see what “require” crashed the whole thing
  3. install some gems (hopefully with the version needed)
  4. repeat

Isn’t Bundler supposed to solve that problem?

Bundler

I have used Bundler with Rails 3. But that’s all configured and just automagically works. In a standalone project, there are a few things you need to do yourself.

First:

> bundle init

All that command did was to create an empty Gemfile.

Open the Gemfile with your favorite editor and add your gem dependencies. Mine looked like this:


# A sample Gemfile
source :gemcutter

gem "dm-core"
gem "dm-timestamps"
gem "dm-sqlite-adapter"

gem "json"

Then, run:

> bundle install

So far, this is all regular Bundler stuff. What about your script?

Bundler knows about all your dependencies, surely it will “require” all I need, right?

Yes and … no.

Bundler Documentation Fail

Here’s a screenshot from Bundler’s documentation

Thank you Bundler, I “require” you and now … huh … I still require all the gems I need?! I doesn’t sound very DRY to me.

What the “bundler/setup” line did was to configure the load path.

And you could do your requires manually…

If I’m writing this it’s because there’s a way. I’m just surprised that the Bundler website doesn’t seem to document this useful feature. If there are good reasons why this is not documented (tradeoffs or something) or, even, the default behavior — we can only guess.

Here’s what your script should do:


require rubygems
require bundler/setup

Bundler.require

The “Bundler.require” line will require all your dependencies.

One last note, do lock (bundle lock) your Gemfile so that the dependency resolution phase is skipped. It will make loading your script much faster. (this also applies to Rails projects)

Read Full Post »

Book Reading Opportunity Cost

Reading a book takes time.

The time you spend reading a book is not spent doing something else.

Opportunity cost: (source)

Benefit, profit, or value of something that must be given up to acquire or achieve something else. Since every resource (land, money, time, etc.) can be put to alternative uses, every action, choice, or decision has an associated opportunity cost.

Does it sound obvious? Lately, however, I’ve made a few such mistakes with respect to some books I bought.

Atlas Shrugged

Atlas Shrugged has been recommended many times, by many different people. Consequently, it raised above my threshold of consciousness and I had decided to buy it, read it and reach my own conclusions about it.

I have been to Chapters and I had remembered about Atlas Shrugged. Of course, it was in the shelves and I looked through it. What sealed the deal was the price: $10! How could I go wrong?!

Where did I go wrong?

It could have been an audio book.

I’m writing this with the book on my lap. It stands at 1069 pages in something that feels like 6-pt font. More so than other books I’ve had, I will feel the impact of the time invested in reading it. Even in audio, Atlas Shrugged stands tall with 63 hours of narration.1

I bought the physical book with good intentions. However, it has been gathering dust for a while now. I wondered when, if ever, I would have enough time to decide to read it.

If only I had bought it in audio format. But it was too late now… I had already bought it in paper format. Buying the audio book meant paying “twice”. There was something very unpleasant about that thought.

Opportunity Cost

Then, a few days ago, I realized that the $15 it would cost me to buy the audio book was not completely lost.

It meant that I could spend the time I listened to the book doing other things: dishes, chores, exercising. It meant I could start the book right away, instead of some perfect moment in the future, because I could do other things at the same time.

Am I happy that I paid twice? No. But the total cost of the book, $25 ($10 paper + $15 audio), must be contrasted against the time I just saved by not having to sit down while reading.

I have a few other books with which I will have to repeat this process. Uncle Tom’s Cabin comes to mind. That was another “cheap” book I bought in the spur of the moment. I’m learning this lesson about the total cost of a book.

Notes

  1. Read Full Post »

Follow

Get every new post delivered to your Inbox.