Category Archives: ruby

Gemsets and .rvmrc – Ruby Gem Management Made Easy

RVM is a tool that automatically manage your ruby versions. You can make 1.8.7 and 1.9.2 live together without issues. The installation process is a bit lacking – bash < <( curl ) is not really a command that looks safe at first sight. Blindly executing code from the Internet is not for the faint of heart but yep, rvm is really that useful.

I especially love gemsets. They allow you to completely separate your various development environments for each of your projects. Gem conflicts are a lot less common.

You can even automatically change to a gemset for each of your project by using the an .rvmrc file. I use ruby 1.9.2 by default but I have one project that uses ruby 1.8.7. I use a separate gemset called ‘mygemset’.

# create the gemset if it does not exist
# switch to default ruby version when exiting directory
# use ruby 1.8.7 with a specific gemset
rvm ruby-1.8.7-p302@mygemset

Pretty neat. Since this file is committed, it also means everyone on the team has the same setup.

Rediscovering ERB (without Rails)

Since I discovered Haml for my HTML templates in Rails, I stopped using ERB. Haml is more intuitive and readable to me.

I am rediscovering ERB though for something else completely – Java code generation.

There is a lot of boilerplate code in most Java projects. A good IDE can help you alleviate some of it but most (if not all) applications will have additional boilerplate very specific to your application. Using ERB, you can easily create cut down on mindless typing.

For example, let’s suppose you are writing many Java listeners. Just create a script that will do it for you. I’ll make an exemple with a ContactListener class that is notified when a user is added, removed or blacklisted from your contact list.

require 'rubygems'
require 'erb'
# setup - could be initialized from script arguments
classname = "Contact"
listener_methods = ["added", "removed", "blackListed"]
# create file based on ''
content ='')
template ="#{classname}", 'w') { |f| f.puts(template.result) }

If I run this using

public interface <%= classname %>Listener {
  <% listener_methods.each do |method|  %>
  void <%= method %>(<%= classname %>Event event);
  <% end %>

Then I generate this file:

public interface ContactListener {
  void added(ContactEvent event);
  void removed(ContactEvent event);
  void blackListed(ContactEvent event);

That was easy – and it’s not that useful I admit. I’m sure you can create your own listeners just as fast in your own IDE. Probably even faster.

However here comes the fun part – the same script can be used to create lots of other classes:

  • The ContactAdapter class
  • The ContactEvent class
  • The ContactController
  • The Contact model
  • The ContactRepository (maybe even with a few methods to add/remove/retrieve a contact by id)
  • The ContactView through which users will be able to trigger ContactEvents (maybe with a simple UI – even buttons to trigger each events on the ContactHandler)
  • Registration of the ContactHandler and the ContactView by your favorite IOC framework
  • Registration of the ContactView so it is accessible from your application’s menu
  • Test classes for all of the above (including failing test cases that you’re going to have to write)

And probably other bits here and there to glue everything together.

Your mileage may vary, but code generation might be a great way to make your path shorter.

Can your IDE do that?

Dear Java, Please Let Me Work

I had to read the content of a file for a ruby project. This is how I did it:

def read_file_content(filename)

Simple enough, right?

Then for some reason I thought about how I would have to the same in Java:

public String readFileContent(String filename) {
  try {
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    StringBuilder buffer = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
    return buffer.toString();
  } catch (IOException e) {
    return "";

Need I really explain why I prefer Ruby these days? Even if you remove the try/catch clause and rethrow the exception, that’s still a lot of code compared to a single line of ruby. I am also aware that it is possible to write the same Java code in a single line using Apache’s commons-io (IOUtils.toString(InputStream) – but still, why are the base Java libraries so verbose for file manipulation?

I still like Java, but I’m impress by the compactness of Ruby. It just works as it should. Sometimes working in Java it seems I’m just smashing the keyboard instead of getting actual work done.

Developer in Training

I believe in software craftsmanship and in training to become better.

That’s why when I was challenged at work to take a few hours each week to train, I thought “What a great opportunity!”.

Well that’s what I should have thought. Instead I said: “But what about the project? The deadline? The world is going to end!”

Then I realized how lucky I was to work at Pyxis. I asked the Product Owner and it wasn’t such a big issue.

Lesson #1: Ask and thou shall receive

For my first training session, I decided to do the some katas in ruby as it has become my language of choice for pet projects.

Part 1 – Primes

I started my first session with the Prime Number kata. I like this kata since it is really simple, can easily be done in less than 30 minutes (closer to 10 once you’ve done it a few times) and I learned a few useful tricks in ruby by watching this kata permormed by Uncle Bob on Katacast.

I plan on doing the Prime Number kata every week as a warm up.

Part 2 – Bloom filters

Afterwards I tried kata #5 from – implementing a Bloom Filter. It’s an algorithm that probabilistically determine if an element is part of a set. It works like this : if the element is in the set, it will always return true. However if the element is not in the set, the bloom filter might still return true depending on some probabilistic stuff.

Training ends

Here are my highlights of the session:

  • I learned a new algorithm, it’s purpose and a few use cases
  • I performed everything in pure TDD
  • Continued to learn new keyboard shortcuts in Rubymine (I try never to use the mouse in katas. If there is a keyboard shortcut I forgot, I look it up and make a point on using it)
  • I managed to take some time to train!

The only downside I can think about is that I spent a little while trying to configure ruby 1.9.2, rspec and the latest beta of Rubymine 2.5 before reverting to ruby 1.8.7.

As for improvements, I see lots of ways to improve the Bloom Filter katas, especially when it comes to my tests. I’ll give it another try next time.

Empower Textmate for Ruby

I’ve been programming a bit in in ruby from Textmate and although the editor is pretty sharp, I thought there were two missing features.

  • Start rspec tests
  • Start the debugger from Textmate (not often useful but I still do it from time to time… maybe it’s my old Java mindset…)

Behold! I found two plugins filling the description! (and do more on the sides)

  • Rubyamp allows me to start the debbuger from Textmate and does a lot more.
  • The rspec bundle allows you to start rspec tests from Textmate

As a bonus I also found TmCodeBrowser which allows you to see all your classes/methods and jump to them.

Power up Textmate with plugins!

I’ll try to update this page if I find other useful plugins.