Comparing array of strings with Ruby

A while ago I made a blogpost on how to compare two arrays with each other. Unfortunately it doesn’t support the comparison of strings in an array. At least until recently I thought that would be the case.
So how is it done? Well lets say you want to start a flamewar between your favorite editor and the you-know-which-one. So you make two lists of either sides:


vim_users = [ "Ann", "Berta", "Charles" ]
emacs_users = [ "Charles", "Donald", "Elane" ]

Now you do know that some like to use both but they will only hinder a good argumentation i.e. flamewar, so how do we get the hardcore users of each group?
If we could use some good old logic it would result in a simple XOR i.e. in Ruby the ‘^’ symbol. This is provided by the set library. So lets get some code to explain this a bit more:


use 'set' #reference the libraries

vim_users = [ "Ann", "Berta", "Charles" ]
emacs_users = [ "Charles", "Donald", "Elane" ]

result_set = vim_users.to_set ^ emacs_users.to_set # Convert and XOR the two arrays

You can even convert the set back to a normal array:


result = result_set.to_a

Now you have your list of people that you can invite to your flamewar. Want to know who will be on which side? Well here is the base logic to get it achieved:


A - A & B # => elements present in A but not in B

In my little example this would look like this:


vim_only_rooters = vim_users.to_set - (vim_users.to_set & emacs_users.to_set)
emacs_only_rooters = emacs_users.to_set - (emacs_users.to_set & vim_users.to_set)

I hope this will help other people than me on how to solve this problem in a *my opinion* nice way.

Advertisements

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.