Become a Code Ninja with Codewars

I am not a ninja, in the traditional sense. My chubby frame and fuzzy beard can attest to that. But Codewars has given me the ability to be a ninja in one very special area of my life: programming.

Kata are moves that one practices and perfects as they attempt to master a martial art. In this spirit, Codewars presents you with hundreds of kata (coding challenges) in the programming language of your choice. As you earn honor points for completing kata, you advance through the “kyu” (ranks).

The concept of programming kata has been around in other forms, but Codewars is an entire platform of crowd-sourced programming challenges. Users are encouraged to create kata of their own that are curated and used by others. It’s a self-perpetuating system (with a lot of hard work from the Codewars staff).

Each kata has a description, a browser-based code editor, and even a small testing library you can use to verify your own solutions. Once you’re confident you’ve solved the kata, you can submit your code to see if it passes the kata author’s own set of tests. Instant gratification!


Why Train?

As I said earlier, programming kata is not a new concept. Years ago, Chad Fowler published a book that stressed the importance of practicing your programming craft. Chad posed an interesting question: you wouldn’t expect to become a great musician with all performance and no practice, so why do we think we can become great developers without challenging ourselves outside of work?

Malcolm Gladwell explored the concept of “10,000 hours” in his book Outliers. The theory is that it takes about 10,000 hours of dedicated practice to become an expert at something. If you’re like most developers, 90% of what you do at work doesn’t count.

Becoming an expert requires constant challenge, and that’s what Codewars provides. They gamify the act of self-improvement, and they do it well – it’s downright addictive!



Programming Kata: Treat a Hash Like a JavaScript Object

In JavaScript, objects look and act an awful lot like hashes:

var hash = {a: 'b', c: 'd'}

hash.a  #=> 'b'
hash['c']  #=> 'd'

How can we mimic this in Ruby? It’s easier than you might think. Ruby classes have an instance method called `method_missing` that is called whenever…you guessed it…it can’t find the method you asked for. Since Ruby allows us to open up any class and extend it, let’s do that now:

class Hash
  def method_missing method_name, *args, &block
    return self[method_name] if has_key?(method_name)
    return self[$1.to_sym] = args[0] if method_name.to_s =~ /^(.*)=$/

Now we have JavaScript object-like power in Ruby hashes:

hash = {:five => 5, :ten => 10}
hash.five  #=> 5
hash.fifteen = 15
hash[:fifteen]  #=> 15

We’re doing three things here. First, we’re checking if the hash has a key by the given method name (as a symbol). If it does, we return the value. Next, if method_name ends in an equal sign, we create a new hash entry with the value specified. Finally, it’s very important to always call `super` at the end of `method_missing`. This way, if the method is still missing, other parents classes up the inheritance chain might provide the solution.

You can practice this as an interactive kata on Codewars.