Learn Ruby through Testing: Ruby Koans
Tags: ruby, testing, beginners
Published: 2020-07-07 00:18:43

One of the best ways (in my opinion) to begin learning to program is through test-driven development. If you're just getting started learning ruby, want to explore more of the fundamental concepts, or learn more about how tests are written in Ruby, there is a great tool available called the Ruby Koans. 

Koan is a word used by Zen Buddhist monks to describe questions, paradoxes, stories, etc. that are to be meditated on and further their Zen practice. The Ruby Koans were created by Jim Weirich who was very influential to the Ruby language community as he developed multiple tools including Rake. The purpose of the Ruby Koans is to introduce users to the Ruby language through test code. Because testing is so important to sustainable and scalable development, learning foundational concepts using test code is a great way for beginners to learn.

<h2>Getting Started</h2>

In order to get started, we need to make sure we have Ruby installed and the version is at least 1.8. To check which version is installed, in the terminal we can run:

```bash
ruby -v
```

I have ruby 2.6.4 installed so we are good to go. <a href="https://www.ruby-lang.org/en/downloads/"><i>Check out the documentation here if you need to install or upgrade Ruby.</i></a>

<h3>Clone the Git Repo</h3>

<a href="https://github.com/edgecase/ruby_koans">The GitHub repo has all the code needed to run the koans.</a> Simply navigate in the terminal into the directory that we want the koans project to be and run:

```bash
git clone https://github.com/edgecase/ruby_koans
```

Then open up the directory in a code editor. (<a href="https://code.visualstudio.com/">I use Visual Studio Code.</a>)

<h3>Generate the Koans</h3>

The koans are not generated by default so before we explore the directory we need to run:

```bash
rake gen
```

If at any point we wish to restart and regenerate the koans, just run:

```bash
rake regen
```

Quick note: You may notice the src directory follows the same structure as the koans directory. That folder does contain the koans but with the answers filled in. You are welcome to reference these answers if you are stuck, but I recommend following the tests first.

<h3>Fix the Tests</h3>

In order to start our journey, we need to run the first test. The command rake will execute our test code.

```bash
rake
```

![ruby koans tests](https://dev-to-uploads.s3.amazonaws.com/i/vh8a2mf5r85eq97s891f.png)

The test output says:

```bash
Please meditate on the following code:
  /Users/laura/Desktop/dev/ruby_koans/koans/about_asserts.rb:10:in `test_assert_truth'
```

So we can navigate to the file in the koans folder titled 'about_asserts' and see that the first method is called test_assert_truth.

![Code with error](https://dev-to-uploads.s3.amazonaws.com/i/wpbz6n6glcsor4d8u2pk.png)

The code says false and there is a note to change it to true. Let's delete false and replace it with true. Now let's run rake again, and we've passed our first test!

![Passed first test](https://dev-to-uploads.s3.amazonaws.com/i/slx598yrhr6t1tlc71ot.png)

Keep going through all the tests and you might achieve enlightenment. More likely, you'll at least have a better understanding of Ruby and how to test your code.

<h3>Note on Length</h3>

The koans usually take 5 or so hours. I completed them in a span of 6 hours with a few breaks between, but I would recommend spreading it out into smaller chunks throughout a few days, week, or even month. There is a lot of information in each answer so make sure you're learning why a line of code passes the test before moving on to the problem. Passing the tests is not as important as learning what they teach. But most of all, have fun coding!