Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with Ruby Koans #6 - What exception has been caught?

Tags:

exception

ruby

I'm trying to learn Ruby through Koans but I'm stuck on the 6th step.

Here's the code:

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  
  # make 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

I know I'm supposed to replace the __ with something to do with the error message "NoMethodError" but I can't seem to figure it out.

This is the error message that I get when I run the "path_to_enlightenment.rb":

The answers you seek...
  <"FILL ME IN"> expected but was  <NoMethodError>.

I would really appreciate some guidance with this - it's driving me insane! I would love to know the answer and a possible explanation. Thank you!

like image 716
vich Avatar asked Sep 29 '10 03:09

vich


People also ask

How long does Ruby koans take?

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.

What is a ruby koan?

The Ruby Koans walk you along the path to enlightenment in order to learn Ruby. The goal is to learn the Ruby language, syntax, structure, and some common functions and libraries. We also teach you culture by basing the koans on tests.


3 Answers

The answer here is "NoMethodError"

you need the items on either side of the , to be equal, therefore making them both ex.class will do that.

Then you'll need to go on to /__/ Below.

like image 159
Elliot Avatar answered Nov 13 '22 12:11

Elliot


I had to put the assert_equal statement into parens to get this one to pass. Must be a bug.

  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
    # make some assertions about it.
    begin
      nil.some_method_nil_doesnt_know_about
    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("undefined method", ex.message)
    end
  end
like image 27
smp Avatar answered Nov 13 '22 13:11

smp


You need to substitute __ with the actual

assert_equal NoMethodError, ex.class 
like image 25
Carlo Ledesma Avatar answered Nov 13 '22 14:11

Carlo Ledesma