I have been developing applications in Java for the last 15 years. I know the language, I know the API, I know all the important frameworks and at one point I even felt arrogant enough to talk about the Java internals at conferences.

But since starting at BetterDoc I’m using Ruby as my main programming language. I still try to sneak in a little bit of Java here and there, but Ruby is what I’m doing most of the time.

It feels a little bit like learning how to crawl again after having been an athlete. It’s a mixture between excitement, frustration, and pure embarrassment.

The story I want to write about in this post falls into the last category: pure embarrassment.

It’s funny to see and to recognize how the usage of one programming language (and one programming paradigm) influences the way of thinking. Some things simply seem too obvious to even think about it.

In my case it was something as simple as replacing parts of a String.

So let’s create a simple use case: I have a String A B C and now I want to replace B with X.

In Java it would work like this:

    String before = "A B C";
    String after = before.replaceAll("B", "X");
    System.out.println(before);   // will print "A B C"
    System.out.println(after);    // will print "A X C"

As replacing a String is a very basic functionality, it didn’t take me long to find a nice Ruby example on Stackoverflow. The code looked something like this:

    before = "A B C"
    after = before.gsub!(/B/, "X")

So far, so good. The “after” variable containes the desired value - at least that’s what I thought.

However, something weird happened that I couldn’t explain to myself: At another location where I used the before variable, suddenly the original value A B C was no longer available.

After a lot of head scratching (“what the hell is happening here?”) and frustration I finally found out, that gsub! will not create a new String with the replaced values but will change the String itself upon which the method is called. Ruby even has the nice convention of indicating that methods ending with an an exclamation mark will modify the object it’s called on.

The method I was actually looking for was gsub (without an exclamation mark) - but as every good Stackoverflow-Copy-And-Paste-Artist I didn’t take the time to fully understand what I was doing.

I learned quite a few things out of this experience:

  • There is no piece of code too small to test. Had I taken the time to write a test (which I obviously didn’t), I quickly would have discovered that something didn’t work out the way I thought it would.
  • Stackoverflow is a wonderful place to get an idea of how to use a piece of code - but especially for these basic things (also) taking a look at the API documentation will give a lot more context.
  • Assumptions that every programming language is based on the same set of rules are pretty dangerous.
  • Knowing one language inside out doesn’t necessarily help you in different environments - it can even mislead you.
  • Be humble when working with beginners. There are moments when I feel like an engineer that has come fresh out of college and doesn’t know anything about what he is doing. Yes, it will get better (at least I strongly hope it will), but just because someone isn’t familiar with every detail doesn’t mean that he is stupid.
  • I have a long way to go until I will feel really comfortable in Ruby - but I’m not ready to give up yet ;-)