Friday, February 29, 2008

Case Insensitive String Comparison is Teh Suck

Why is it so verbose to compare two strings in C# and see if they are equal, while ignoring case?

What I mean is this:  ("Apples" == "apples")  => false

Take the following examples, each of which does what I want it to, however there is so much noise code here that it really takes away from the readability of the code:

Assert.That(matches[0].Email.Equals(email, StringComparison.InvariantCultureIgnoreCase));

Assert.That(matches[0].Email.ToLower().Equals(email.ToLower()));
Assert.That(string.Compare(matches[0].Email, email) == 0);
Assert.That(Regex.Match(matches[0].Email, email, RegexOptions.IgnoreCase).Success);

Each one of these works, but I really wish this was a language feature.  Wouldn't it be nice to have a new operator syntax?
Assert.That(matches[0].Email ==? email);
//or
Assert.That(matches[0].Email ==~ email);
It turns out that we're not alone.  Ruby and Python have the same problem, however their regular expressions option is much more elegant looking (due to the languages having regex as a core feature).  This seems like such a common need that I'm surprised that this type of thing isn't built into the core of most languages.

Granted, there are concerns about how each one of these would behave, for example, how do you compare strings from two different cultures?  In my case I'm always dealing with English characters, but I fully understand the need to support internationalization.

What do you think?  Would you welcome a new operator to save your self some typing and improve readability?

Thursday, August 23, 2007

e - Text Editor - no such file exists -- ubygems

There's no doubt that e kicks ass.  On one of my computers, however I was having a problem using some of the bundles.  Specifically some of the bundles utilize cygwin to do their magic.

I was receiving this weird error, where ruby is complaining that it can't locat a file named "ubygems".  Obviously, they are referring to rubygems, and the problem exists when you have a windows install of ruby and a cygwin install of ruby.

To fix it you need to reinstall ruby gems within cygwin.  Download the latest version of rubygems from rubyforge.  Put the tarball in the c:\cygwin\home\USER folder, then open up your cygwin prompt and type:
$ tar -x -f rubygems-0.9.4.tgz  (this will change if you download a different version)
$ cd rubygems-0.9.4
$ unset RUBYOPT
$ ruby setup.rb

At this point rubygems should be installing and you should be good to go.  Back to "e goodness"!