JRuby, not in its setting (or configuring jirb under Windows)

For a good while now, I’ve been using Ubuntu on my main home machine. Working with Ruby and JRuby on Linux is a charm.

Tonight, as I tried to set myself up to work with JRuby on Windows*, I bumped into an annoying problem. When I tried to configure jirb, the JRuby version of IRB, it didn’t always seem to find the .irbrc config file in my home directory. I looked a bit on the ‘net and found nothing that helped me solve my problem. I’ve worked out a fix, you can follow along in this wonderful adventure.

So to configure irb, I create the .irbrc file in my home directory.

C:\Documents and Settings\Mat.irbrc

Note: Windows will yell at you if you try to make a new file starting with a “.” in windows explorer. Just use any good text editor to create it directly.

In it I put some configs for auto-completion and auto indent, as suggested on the Tips and Tricks page on the Rails wiki:

IRB.conf[:AUTO_INDENT] = true IRB.conf[:USE_READLINE] = true require 'irb/completion' puts "Yay! Completion's loaded!" #Uhh, that's my special debugging code :-)

Now I fire up a console and start the interactive Ruby shell with

C:\Documents and Settings\Mat>jirb Yay! Completion's loaded! irb(main):001:0>

Hmmm, it works. What am I yelling about? :-) Let’s say we fire it up in an actual project directory instead.

C:\projects\an_actual_project>jirb irb(main):001:0>

Oops! My “special” debugging code isn’t executing now :-)

If you care to see what’s the code that hints at how to solve the problem, you can open

…\jruby-1.0.1\lib\ruby\1.8\irb\init.rb

Or check it out a snippet of IRB.rc_file_generators online.

As we can see, if the HOME environment variable is set, the method looks for .irbrc in the directory, otherwise it looks for it in the pwd (present working directory).

Nice! That explains why it works in my home and not elsewhere :-)

So now I just have to set a HOME variable and I’m all set:

home.png

Now after restarting my console (to have the updated environment variables):

C:\projects\an_actual_project>jirb Yay! Completion's loaded! irb(main):001:0>

Note that even though Windows variables are not case sensitive, you must name your HOME variable in capital letters. Ruby expects the ENV hash to contain the HOME variable in caps.

  • Q: Why would I want to set this up on Windows, if I’ve got a perfectly good Linux setup at home?

A: I’ll be giving a talk about JRuby at the next Montreal on Rails, and since I haven’t really needed a laptop in at least two years, well, I only have 3 years old laptop. This old geezer can run equally old versions of Linux, but I wouldn’t like my audience to have to suffer the visually challenged distro I currently have on there.

Comments