Setting bash environment variables in Ruby

Ruby scripts can be great every day helpers. I recently run over a task in which I wanted to start an application that relied on exported variables. In Ruby one can easily do this via the ENV object.

ENV['ENV_VAR_NAME'] = "some/lib/path"

You can make this of course more dynamic by using the ARGV for command line arguments or config files. But I think you get the idea from the above example.

Please note that it is not possible under Ruby to export a variable to its parent process i.e. the caller shell with the example above. I do also not know of any way how that would be achieved.

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.

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.

SVN revert equal in Git

Using git, coming from SVN and want to revert changes on a file? In SVN it used to be:

svn revert filename

Well revert in git isn’t what you want to do in this case. Due to it’s local repository you have to think a bit different here. When checking out you get the latest committed version from your local repository, so what you actually have to do is the following:

git checkout filename

Now you have the equivalent to the gold old revert.

git svn status

Working with git-svn and need to know what your svn status would look like? Well within two lines you could be there. First make sure that your remotes/git-svn is up to date:

git svn fetch

Now you can a diff over that branch:

git diff --name-status remotes/git-svn

You should now see your standard diff. In case this didn’t work or your not sure where your default branch is lying, enter the command:

git branch -a

This will show you all your local and remotely tracked branches. Which looked in my case like this:


* master
remotes/git-svn

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:

some_string.to_s.empty?

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.