Ruby Intro - Debugging Flashcards
How do you use the “class” method?
Tells you what kind of class an object is:”string”.class # => String3.class # => Fixnum
What is difference between inspect and to_s?
Inspect returns valid ruby code for debugging purposes (always surrounded by quotes). to_s transforms to string and is used as part of program (sometimes in quotes).[1, 2, 3].to_s # => “[1, 2, 3]”“my_string”.to_s # => “my_string” (already a string)nil.to_s # => “” (nil represented as nothing, or emptiness)[1, 2, 3].inspect # => “[1, 2, 3]” (same as to_s)”my_string”.inspect # => ‘“my_string”’ (notice the quotes)nil.inspect # => “nil”
What is the difference between puts and p?
Puts is like to_s. It prints out the string. ‘p’ is like inspect. It prints out the ruby code for debugging purposes (always in quotes).puts “my string!” # prints “my string!”p “my string!” # prints ‘“my string!”‘puts nil # => prints a blank linep nil # => prints “nil”
What is method to boolean test for class of an object?
is_a?(parameter)”string”.is_a?(String) # => true”string”.is_a?(Object) # => true
What is technical debt?
In the rush to complete projects, bad design is sometimes a compromise made to finish a project on-time.
What is ideal code designed for easy debugging?
Code that is well decomposed into discrete methods.
in debugging, what is a “stack trace?”
These are the lines that tell you where in the code line is an error.
What is a “nameError” exception?
A NameError exception is thrown when you try to use a variable or method that hasn’t been defined. Could be the result of mistyping the name of a variable or method.
NameError: undefined local variable or method `favorite_nmber’ for
What is an “Uninitialized constant” exception?
It is a type of NameError exception which means that Ruby is trying to find a class (or other constant), but didn't find it. This could be because of a class name typo, or often because we forgot to require the Ruby file that loads the class. You'll get this variation if the name starts with an upper-case letter, since in Ruby variables/methods start with lower case letters, and classes start with upper case letters.
NameError: uninitialized constant UnloadedClass
What is “NoMethodError” exception?
This is similar to NameError; in fact it’s a subclass. This is thrown when it’s clear the user wanted to call a method (didn’t try to look up a variable) that doesn’t exist. A particularly common error occurs when a variable is nil when we don’t expect this
NoMethodError: undefined method `my_fantasy_method’ for “my string”:String
NoMethodError: undefined method `[]’ for nil:NilClass
nil is an instance of the class NilClass, which doesn’t have the method we want.
What is “ArgumentError: wrong number of arguments” exception?
If we don’t give a method the right number of arguments, we will get an exception thrown at us:
[12] pry(main)> [1, 2, 3].shuffle(“unwanted argument”)
ArgumentError: wrong number of arguments (1 for 0)
from: (pry):9:in `shuffle’
Here we give the shuffle method an argument when it doesn’t take one. Ruby throws an ArgumentError exception back at us; it tells us that we passed the wrong number of arguments. It even says that we passed 1 argument when 0 were expected.
What is a “TypeError” exception?
A TypeError may be thrown if you pass the wrong type of thing to a method. For instance, numbers can only add other numbers
[8] pry(main)> 2 + “”
TypeError: String can’t be coerced into Fixnum
Here, we try to add a String to a number (Fixnum is the standard integer class). The method + works by first trying to turn its argument into a Fixnum, then adding it. A String cannot be turned into a Fixnum (coerced), so the method complains.
What is a “LoadError” exception?
There are two very common causes. Sometimes you are trying to load a file that is provided by a gem, but you haven’t installed the gem yet.
Another common cause is that you are trying to load another source file in your project, but you forgot the initial ‘./’. “Relative” includes are used to include files that are inside your project, you write them like this:
LoadError: cannot load such file – primes.rb
What is a “SyntaxError” exception?
The most common are forgetting to close quotes, parentheses, or do-end blocks.
syntax error, unexpected $end, expecting keyword_end
“$end” means end of the program