DEV Community

Laura Berge
Laura Berge

Posted on • Updated on

Learn Ruby through Testing: Ruby Koans

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.

Getting Started

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:

ruby -v

I have ruby 2.6.4 installed so we are good to go. Check out the documentation here if you need to install or upgrade Ruby.

Clone the Git Repo

The GitHub repo has all the code needed to run the koans. Simply navigate in the terminal into the directory that we want the koans project to be and run:

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

Then open up the directory in a code editor. (I use Visual Studio Code.)

Generate the Koans

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

rake gen

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

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.

Fix the Tests

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

rake

ruby koans tests

The test output says:

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

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

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.

Note on Length

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!

Top comments (4)

Collapse
 
wulymammoth profile image
David

I completely forgot about koans for programming languages. I don't think I ever got much out of them, as the zenful language confused me at times, but perhaps it's my experience. But I am curious about what you found most useful about koans compared to other resources and what you found lacking about them. It'd be useful for me to figure out when I should still recommend them.

My two favorite resources to practice after going through some syntax and features of a new language are: Exercism.io and (gah) Leetcode.

Collapse
 
lberge17 profile image
Laura Berge

Thanks for reading! I personally like using the ruby koans for learning Ruby's idiosyncrasies. I think it's a good intro to complete beginners since the answers are available if they get completely stuck, but more than that I think they are great for programmers who already have experience in other languages. It does a great job explaining more about how the language is constructed on a foundational level.

That being said, I think if one is looking more for algorithm practice or learning the depth of available methods, other resources like Leetcode are more suited. Also, the Ruby Koans only take about 5 or so hours, so they focus more on foundational knowledge rather problem-solving.

Hope that helps!

Collapse
 
briankephart profile image
Brian Kephart

I used RubyKoans when I was first learning. I recommend them every chance I get. You do a great job presenting them here.

You mentioned understanding RSpec, but I thought the koans used MiniTest, which is bundled with the Ruby language. Am I mistaken about that? Or are there different versions?

Collapse
 
lberge17 profile image
Laura Berge

You are completely correct, RSpec has a completely different syntax as well. I definitely have more to learn about testing, especially in Ruby. Thanks for the catch! I will edit the post.