Skip to content

Instantly share code, notes, and snippets.

@ryandhaase
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save ryandhaase/8c1587b42c02aefad7c1 to your computer and use it in GitHub Desktop.

Select an option

Save ryandhaase/8c1587b42c02aefad7c1 to your computer and use it in GitHub Desktop.
Debugging Code - Error Types

###NoMethodError

NoMethodError: undefined method `hello` for main:Object

Means that we tried to invoke the hello method, which has yet to be defined

###Wrong Number of Arguments - ArgumentError

ArgumentError
wrong number of arguments (2 for 1)
exercise.rb:1:in `hello'

exercise_spec.rb:5:in 'block (2 levels) in <top (required)>'

This tells us that the method hello is being invoked with 2 arguments but the method definition only takes 1

###NameError

NameError
undefined local variable or method `firs' for #<RSpec::Core::ExampleGroup::Nested_1:0x007f30f9b42360>
exercise.rb:2:in `hello'

exercise_spec.rb:5:in 'block (2 levels) in <top (required)>'

Telling us that there is most likely a typo in one of our variables or the variable is undefined

###TypeError

def add(a,b)
  a + " + " + b + " = " + (a + b)
end

Specs:

describe "add" do
  it "returns a string with 1 and 2 added" do
    expect( add(1,2) ).to eq("1 + 2 = 3")
  end
  it "returns a string with 5 and 7 added" do
    expect( add(5,7) ).to eq("5 + 7 = 12")
  end
end

Error given:

TypeError
String can't be coerced into Fixnum
exercise.rb:2:in `+'

exercise.rb:2:in `add'

exercise_spec.rb:5:in `block (2 levels) in <top (required)>'

Tells us that the string can't be converted into a number, try using string interpolation such as:

def add(a,b)
  "#{a} + #{b} = #{a + b}"
end

###syntax error

def hello
  "Hello World"

Error given

exercise_spec.rb:2:in `require': exercise.rb:2: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError) from exercise_spec.rb:2:in `<top (required)>'

syntax error's tells us we are missing or have an extra keyword, or character somewhere. In this case we are missing end in our hello method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment