Unittesting Ruby 1.9 in RubyMine 3.1.1

I like work with RubyMine 3.1.1 due to the easy way I can debug, browse through different files, checks etc. all the good stuff you only get with an IDE.
Further I really try to do TDD or at least write tests after implementing some new code – and hey isn’t there that nice feature within RubyMine that allows you to run all your tests? Yes there is but when you run it under Ruby 1.9.x you will get the IDE telling you that your tests failed (even though in the console window you can see all your tests have passed..). So why is that and how to fix?

The reason why this happens is because as of Ruby 1.9 test-unit is not anymore within the default package of rails. It made room for the more lightweight MiniTest. But that isn’t much of a worry download TestUnit via gem install (or click your way through RubyMine if you want..) and voilĂ  all your unit tests will work as expected (or not depending on the status of your code..).

Coming this far on your own is not that much of a problem, mainly because the RubyMine tells you it is missing TestUnit. But how do you get the “Run all tests..” working again? In your local Gemfile (should be in the root directory of your project, you may have to create it on your own if your writing a Rails 3.0.x application) add the following line:

require 'test-unit'

Now you can run all your unit tests within RubyMine and get the nerve relieving green after all tests have run through successfully.


Ruby check if string is null or empty

Coming from C# I liked the Frameworks implemented option to check in one go if a given string is null or empty.

Now in Ruby the string class has the method empty? which checks just that i.e. null or empty. But due to the dynamic nature of Ruby one has to tell the interpreter that one would like to have the object as string before you can access the method.

The following code is an example on how it’s done:


Ruby print number with leading zeros

When needing to print out a number with leading 0 the easiest way I found was:

some_number = 3

result_string = "%02d" % some_number # => "03"

After the percent symbol the 2 tells ruby the minimal length of the number and the 0 tells it what to put in if the length is not met (default is a whitespace i.e. space).

Should a larger number be passed to the output format for example 1234, the output will be “1234”, so it will not be shortened.

Compare lists/arrays in Ruby

Ever stumbled over the problem, that you have two lists (for clarities sake, list A and B) and want to know what elements are missing from list B that are in list A?

With Ruby this problem can solved in a rather elegant way, by using set algebra. Let me show you how with a few examples. For the remainder of this blogpost I will assume the following values for our two lists:

list_a = [ :a, :b, :c ]

list_b = [ :b, :c, :d ]

Now lets assume you only want to have the items that are missing in list A. You can get the elements like this:

list_a - ( list_a & list_b ) # => :d:a

The analog is true for B. And if you want to find the elements that are either only in A or B you can do just as easily:

( list_a | list_b ) - ( list_a & list_b ) # => :a, :d