Ruby Intro - Debugging Flashcards

You may prefer our related Brainscape-certified flashcards:
2
Q

How do you use the “class” method?

A

Tells you what kind of class an object is:”string”.class # => String3.class # => Fixnum

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is difference between inspect and to_s?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between puts and p?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is method to boolean test for class of an object?

A

is_a?(parameter)”string”.is_a?(String) # => true”string”.is_a?(Object) # => true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is technical debt?

A

In the rush to complete projects, bad design is sometimes a compromise made to finish a project on-time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is ideal code designed for easy debugging?

A

Code that is well decomposed into discrete methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

in debugging, what is a “stack trace?”

A

These are the lines that tell you where in the code line is an error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a “nameError” exception?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an “Uninitialized constant” exception?

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is “NoMethodError” exception?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is “ArgumentError: wrong number of arguments” exception?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a “TypeError” exception?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a “LoadError” exception?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a “SyntaxError” exception?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly