Ruby Koans Answers

Looking for the answers to Ruby Koans? Well you’re not going to get them here! But what I will do is show you how to get them and who to ask if you get stuck.

If you’re anything like me, you’re probably thinking the answers are harder than what they actually are – or that perhaps the questions are trick questions! But they’re not, all you need to do is just answer the questions in the simplest possible form.

For example, you’re asked if nil is an object:

 def test_nil_is_an_object
   assert_equal __, nil.is_a?(Object), "Unlike NULL in other languages"
 end

And all you have to do is answer the question (but in Ruby terms). Is nil an object? The answer of course is yes – so in Ruby language, that is ‘true’.

In fact if you get stuck Ruby will even give you the answers, open IRB and ask away:

nil.is_a?(Object)

And it will say:

=> true

Here’s another example:

  def test_objects_can_be_converted_to_strings
    assert_equal __, 123.to_s
    assert_equal __, nil.to_s
  end

And the answers are:

  def test_objects_can_be_converted_to_strings
    assert_equal "123", 123.to_s
    assert_equal "", nil.to_s
  end

Enjoy!