Topic: Failed Case For me.. not for you!?

Test Case from - Ruby Koans Tutorial

  def test_accessing_hashes_with_fetch
    hash = { :one => "uno" }
    assert_equal "uno", hash.fetch(:one)
    assert_raise(IndexError) do
      hash.fetch(:doesnt_exist)
    end

    # THINK ABOUT IT:
    #
    # Why might you want to use #fetch instead of #[] when accessing hash keys?
  end

This test case should pass (according to multiple online "correct answer" sources). What I get instead is the error below. Please explain what might be causing this issue smile

  [IndexError] exception expected, not  Class: <KeyError>  Message: <"key not found: :doesnt_exist">  ---Backtrace---  /home/user1/Koans/koans/about_hashes.rb:27:in `fetch'  /home/user1/Koans/koans/about_hashes.rb:27:in `block in test_accessing_hashes_with_fetch'  ---------------

Last edited by IcedBoots (2013-04-17 15:14:25)

Re: Failed Case For me.. not for you!?

This is Ruby 1.8 code that you are running on Ruby 1.9+

Try this:
def test_accessing_hashes_with_fetch
    hash = { :one => "uno" }
    assert_equal "uno", hash.fetch(:one)
    error = RUBY_VERSION =~ /1.8/ ? IndexError : KeyError
    assert_raise(error) do
      hash.fetch(:doesnt_exist)
    end

    # THINK ABOUT IT:
    #
    # Why might you want to use #fetch instead of #[] when accessing hash keys?
  end