A Live Developer Journal

Ruby koans

Working through these Ruby Koans

The awesome thing about these 282 koans is that you have to pass them in the style of test-driven development. You open the koan course 'path_to_enlightenment.rb' file and it gives you a failing error message that you have to pass. The error tells you which file to open and what line the error is found on. Then you open that file and there will be a comment on that line with a hint of what to do.

1. About asserts: Truthiness

Error message

Failed assertion

Code to change


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

Passing


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

Observations

2. About Asserts: Messages

Error message

This should be true -- Please fix this

Code to change


  # Enlightenment may be more easily achieved with appropriate
  # messages.
  def test_assert_with_message
    assert false, "This should be true -- Please fix this"
  end

Passing


  # Enlightenment may be more easily achieved with appropriate
  # messages.
  def test_assert_with_message
    assert true, "This should be true -- Please fix this"
  end

Observations

3. About Asserts: equality

Error message

true

Code to change


  # To understand reality, we must compare our expectations against
  # reality.
  def test_assert_equality
    expected_value = __
    actual_value = 1 + 1

    assert expected_value == actual_value
  end

Passing


  # To understand reality, we must compare our expectations against
  # reality.
  def test_assert_equality
    expected_value = 2
    actual_value = 1 + 1

    assert expected_value == actual_value
  end

Observations

4. About Asserts: better way of asserting equality.

Error message

Expected "FILL ME IN" to equal 2

Code to change


  # Some ways of asserting equality are better than others.
  def test_a_better_way_of_asserting_equality
    expected_value = __
    actual_value = 1 + 1

    assert_equal expected_value, actual_value
  end

Passing


  # Some ways of asserting equality are better than others.
  def test_a_better_way_of_asserting_equality
    expected_value = 2
    actual_value = 1 + 1

    assert_equal expected_value, actual_value
  end

Observations

5. About Asserts: Fill in Values

Error message

Expected "FILL ME IN" to equal 2

Code to change


  # Sometimes we will ask you to fill in the values
  def test_fill_in_values
    assert_equal __, 1 + 1
  end

Passing


  # Sometimes we will ask you to fill in the values
  def test_fill_in_values
    assert_equal 2, 1 + 1
  end

Observations

6. About Nill

Error message

Unlike NULL in other languages.

Code to change


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

Passing


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

Observations

7. About Nil

Error message

Expected "FILL ME IN" to equal NoMethodError

Code to change


  def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
    # What happens when you call a method that doesn't exist.  The
    # following begin/rescue/end code block captures the exception and
    # makes some assertions about it.
    begin
      nil.some_method_nil_doesnt_know_about
    rescue Exception => ex
      # What exception has been caught?
      assert_equal __, ex.class

      # What message was attached to the exception?
      # (HINT: replace __ with part of the error message.)
      assert_match(/__/, ex.message)
    end
  end

Passing



  def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
    # What happens when you call a method that doesn't exist.  The
    # following begin/rescue/end code block captures the exception and
    # makes some assertions about it.
    begin
      nil.NoMethodError
    rescue Exception => ex
      # What exception has been caught?
      assert_equal NoMethodError, ex.class

      # What message was attached to the exception?
      # (HINT: replace __ with part of the error message.)
      assert_match(/NoMethodError/, ex.message)
    end
  end

Observations