Koans Games

May 12, 2014

For anyone who already had some programming experience and wants to play with Ruby but somehow impeded by, or just fail to find any logics but coarse information in the so called Ruby Handbook, Koans may be a amazing alternative for you to try.

I’m always wandering that, as a dynamic and reflective programming language, Ruby is fully capable to explains itself clearly, without any chunky handbook. Ruby is designed for programmer productivity and fun, and with this purpose it’s syntaxs and method names are meticulously considered to be intuitive, showing the similar form of nature language, which thousands of elegant Ruby one liners assured. I’m gonna show you an example.

1 # to make every word in a paragraph capitalized
2 @paragraph.split.map!(&:capitalize).join(' ')
3 
4 # a shorter and more skillful approach
5 @paragraph.gsub!(/\S+/,&:capitalize)

From my perspective, instigating somebody to reading the alleged Ruby Handbook is ruining the language, as Jim Weirich and Joe O’Brien, the author of Koans, may agree with. However, they didn’t just hold this point of view, but ultimately build up this helper, which earned my respect spontaneously.

So, if I’ve already attracted your attending, and you are ready for this journey. Then follow me, I’ll point out a path to enlightenment that leading you to cross the Gate of Ruby and witness it’s inner beauty.

Preparation

To start our journey, we must assume that all the equipment assured. This time, we only need two, Ruby interpreter and Rake. If you do not already have Ruby setup, please visit http://ruby-lang.org/en/downloads for operating system specific instructions. For Rake, visit https://rubygems.org/ to get gems, the Ruby’s premier packaging system, and type gem install rake in your console.

And please make sure the version of your Ruby is greater than 1.8. To look up your Ruby version, please type ruby --version.

When everything are ready, please visit http://rubykoans.com/ and get the Koans. Usually you will have a compressed package downloaded, decompress it and enter the target folder. See the list of .rb files? Those are where we will visit later. But don’t be hurry, we need a experienced guide introduce us all the resorts. type rake in your console and see what will hapen.

First try

If everything goes right, you will get the following output:

 1 /YourRubyPath/ruby path_to_enlightenment.rb
 2 AboutAsserts#test_assert_truth has damaged your karma.
 3 
 4 The Master says:
 5   You have not yet reached enlightenment.
 6 
 7 The answers you seek...
 8   Failed assertion.
 9 
10 Please meditate on the following code:
11   /Path/To/koans/about_asserts.rb:10:in `test_assert_truth'
12 
13 mountains are merely mountains
14 your path thus far [X_________________________________________________] 0/282

As you may noticed, the 0/282 is your progress, which indicating how many tests you have passed among all. And where to start? /Path/To/koans/about_asserts.rb:10 is leading you the way. Open the file and you will see the code.

1 # We shall contemplate truth by testing reality, via asserts.
2   def test_assert_truth
3     assert false                # This should be true
4   end

Our journey is made up of several pieces of test, defined as a method whose name starts with ‘test’. The path to enlightenment is filled with such tests and we need to pass them one by one.

For the first test, we encounter the assert method, which have the same meaning as it in any other languages. We have to pass a true to it to pass it. So change the false to true, save the file and type rake again. You will see your progress in the output. BTW, assert is not a Ruby intrinsic method, it is defined in neo.rb file.

All the tests are carefully designed to cover major syntaxs and conventions in Ruby. Some simply need to have the correct answer substituted for an incorrect one. Some, however, require you to supply your own answer. If you see the method __ (a double underscore) listed, it is a hint to you to supply your own code in order to make it work correctly.

More In-depth

Passed all the test with Ruby’s magic still lingering in your head? You may wanna see more about Koans and it’s magic. Open the file neo.rb, and enjoy it.

In this file, all the test mechanism are clearly defined. You can find everything you need to explain Koans, about how it call all the methods started with ‘test’, about how it avoided printing the same progress information twice, about the magic word ‘FILL ME IN’ as you may have been encounted. And what more, once you know it, you can have your own.

If you encountered some problems, you may want to take a look at http://rubykoans.com/ – the official site of Koans. Please feel free to contact me too. I’ll be glad to give out an answer for you.

Keyboard Stroke Frequency Analysis

Count and show keyboard strokes.

About VIM and It’s Philosophy

Published on May 01, 2014

Nachos Notes -- Preparations

Published on February 25, 2014