the handy bundle-exec

I found myself in a pretty confusing situation today when I was trying out Rails 3.1.rc1. In my Gemfile, I specified Compass to be installed from the github repo and bundler did just that. But I had to run Compass's executable in my rails app and I had no clue as to how to do it, because my rvm gemset for trying the RC version of Rails doesn't have Compass installed in the gem environment. So running the following command, generated a command not found error.

Bundler installs it local to the application, when you choose to install from the git repo, so the executables aren't in your gem path. In such situations, bundle-exec comes handy.

Will run the compass command with it's arguments in the scope of the bundle (I'm referring to the gem dependency environment managed by bundler not to git-bundle).

 

Posted
 

The binding method in ruby

I stumbled upon the binding method in Ruby.

Try Binding.new and it'll throw an error. It seems that the only way you can get an instance of the Binding class is by calling the binding method. I'm sure this method calls the Object.binding method either directly or indirectly. Try Object.binding and i'll say that its a private method.

So what's binding?

binding contains the scope of the current ruby block and allows accessing stuff that would else not be in scope. For example,

And you get a NameError. (I love the errors in Ruby. They are well named and are a pleasure to read.)

Modify the say method to accept an argument. Change the puts to eval a ruby expression and pass the binding as second argument to it. When the binding instance is passed, eval evaluates the expression in the scope of the binding.

Now try passing the scope to the say method and see how msg is accessed by magic

Posted
 

Rails 3.1.0 beta1

ERROR:  While generating documentation for activesupport-3.1.0.beta1

... MESSAGE:   incompatible character encodings: UTF-8 and ASCII-8BIT
... RDOC args: --op /home/akashmanohar/.rvm/gems/ruby-1.9.2-p180@rails31/doc/activesupport-3.1.0.beta1/rdoc lib --title activesupport-3.1.0.beta1 Documentation --quiet

That's what you get when you have rubygems 1.7.2. Just do a 

gem update --system

to get 1.8.0 (or whatever is the latest by your date) and the install will go smooth.

Could not find a JavaScript runtime (ExecJS::RuntimeError)

That happens when the ExecJS gem cannot find any javascript runtime installed on your computer. Just install nodejs for now and the error should disappear. I usually choose to install nodejs from the github repo. That way it's easier to get the latest and greatest quickly, rather than having to download and install a new pkg everytime.

And then go generate an app and run the dev server, it'll work. But just don't expect to checkout details of the changes to the frontend (the sass, coffee-script stuff). If you open app/assets/javascript/application.css you might notice a FIXME message. Things aren't that clear to me. Maybe I'll have to spend more time reading the available docs or I'll checkout the next beta (or the RC) and try creating an app with that. Like the release blog post on the rails blog mentions, there isn't enough documentation right now.

 

Posted
 

Automatically creating and switching gemsets for projects

I just forked a very useful trick from tundrax on the #ruby IRC.

Adding that to your project's dir's .rvmrc will automatically create (if necessary) and also automatically switch to the respective gemset, when you cd into the project's dir.

Posted
 

Quickly finding model associations in rails

In Jan-2011, at my internship, I got a chance to work on a really complex application. And I had to get upto speed with everyone else by understanding the app and the reasons behind certain decisions. The one i worked on had more than two dozen models. Thanks to my commandLineFu, I accidentally, found a trick to list associations between models in a rails app in a readable way. And it's very simple.

From the root of the app (can be done from anywhere, but i'll just say I'm in the root of the app).

grep -ri "has_many" ./

Assuming you don't have the text "has_many" anywhere else in the app. If you do, just change ./ to app/models
The same can be done for belongs_to

grep -ri "belongs_to" ./

You'll be surprised to find that bash (ubuntu guy here!) gives you a really nice readable view of the associations. Each line, when read fully, shouts out the association :)
Posted
 

Ruby's method_missing() and define_method()

In case you don’t know about method_missing(), here is how it works. Imagine that you’re calling a method on a Ruby object. For example, you call duck.sing("Quacking in the Rain"). Usually, at this point one of two things happens: if duck has a sing() method that takes one argument, then Ruby executes the method; if duck doesn’t have that method, then Ruby raises an error. But wait: there is a third possibility. If duckdoesn’t have a method named sing(), but it does have a method named method_missing(), then Ruby executes method_missing() instead. Ruby also tells to method_missing() which method you originally called, with arguments and all.

Just found this awesome post on method_missing() in a blog post here. In the comments, there's another good-ish discussion about define_method() vs method_missing()

I completed 107 of the 269 ruby koans this morning. The koans are addictive. I hope to complete them by this weekend and move on to some real world stuff. There's a gold mine of good and interesting stuff in the koans. F.ex: the object_id of boolean values, representation of strings in ruby v1.8 and v1.9, etc

I wish every language has koans. I found functional koans for a bunch of languages and made it a point to search for koans for any language before learning it. However, I haven't found the time to write any interesting stuff in Ruby except rails apps. This vacation seems to be a nice time to port my Arduino prototyping API to Ruby.

noteToSelf: checkout rails engines

Posted
 

Time to get better with ri and rdoc

All these days, googling was my primary method to search how to use tools/gems. I always used to find sample code which I could just read the re-implement in my programs.

I decided to improve that with Ruby. I learnt to use ri and rdoc. It's damn simple. For rdocs, do

gem server

and you can access the docs at http://localhost:8808. It binds to address 0.0.0.0 and not 127.0.0.1, which implies it's going to be available to computers connecting to that address using your IP. Makes sense when you want to run a server dedicated to docs in a networked office :)

ri <string>

Run that and you can search the ri for class and method names.

P.S: As a bonus, there's Radar's Lookup

Posted