Beginners in programming inevitably take a long time to read error messages. For the time being, I tend to copy the full text of the quickest error message and google it. However, if you google, you will get some information like that, but you cannot say that it is the correct information. As pointed out in "Introduction to Ruby for Professionals", even though it seems that it was solved successfully by executing the googled result, it actually created a serious security flaw. It seems that there are times. So, first of all, I think it is necessary to be able to read the error message and be able to extract the problem by yourself. I wrote this article because I thought it would help me get rid of bugs and errors fairly quickly and understand the structure of Ruby.
Referenced → [Introduction to Ruby for those who aim to become professionals](https://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%82%92%E7% 9B% AE% E6% 8C% 87% E3% 81% 99% E4% BA% BA% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AERuby% E5% 85% A5% E9% 96% 80-% E8% A8% 80% E8% AA% 9E% E4% BB% 95% E6% A7% 98% E3% 81% 8B% E3% 82% 89% E3% 83% 86 % E3% 82% B9% E3% 83% 88% E9% A7% 86% E5% 8B% 95% E9% 96% 8B% E7% 99% BA% E3% 83% BB% E3% 83% 87% E3 % 83% 90% E3% 83% 83% E3% 82% B0% E6% 8A% 80% E6% B3% 95% E3% 81% BE% E3% 81% A7-Software-Design-plus% E3% 82 % B7% E3% 83% AA% E3% 83% BC% E3% 82% BA / dp / 4774193976)
If an error occurs during program execution, data showing the method call status is output. This is called a backtrace. Here, I dare to enter the wrong syntax and call the backtrace.
irb(main):001:0> puts hoge
Traceback (most recent call last):
5: from /(Path where Ruby is installed)/irb:23:in `<main>'
4: from /(Path where Ruby is installed)/irb:23:in `load'
3: from /(Path where Ruby is installed)/irb:11:in `<top (required)>'
2: from (irb):1
1: from (irb):2:in `rescue in irb_binding'
NameError (undefined local variable or method `hoge' for main:Object)
The above is the backtrace output because I entered the wrong syntax puts hoge
in irb (Ruby that can be executed on the terminal). This is a very simple error that occurred because hoge
could not be recognized as a character string by enclosing it in single quotes (') or double quotes ("). Let's take a closer look at each one.
First
Traceback (most recent call last):
However, this is not an error message, but rather a traceback (the last latest call), that is, Ruby is executed and the latest execution process up to the error is displayed next. And then
5: from /(Path where Ruby is installed)/irb:23:in `<main>'
4: from /(Path where Ruby is installed)/irb:23:in `load'
3: from /(Path where Ruby is installed)/irb:11:in `<top (required)>'
2: from (irb):1
1: from (irb):2:in `rescue in irb_binding'
However, the execution process up to the error is displayed in the flow from 5: to 1:. The lower you go, the closer you are to the error (in other words, the closer you are to the latest). Here, from 5: to 3:, it means "execution process of irb program", and in the following 2:, it means "execute the first line of irb". I checked 1: but didn't understand the meaning of rescue in irb_binding
. Does that mean "is it supplemented by the second line of irb?"
Next,
NameError (undefined local variable or method `hoge' for main:Object)
This is the error message. The first is NameError
, which is the type of error message. The types of error messages will be explained later. The following is ʻundefined local variable or method
hoge'for main: Object, but since
hoge`` cannot be recognized as a string, it is searched from a local variable or constant, and it is not defined. It means that · · ·
This time it's a backtrace due to a simple mistake, so it only takes 5 lines, but if it gets complicated, you may see dozens of lines of execution. Even in such a case, it is necessary to read the contents calmly and carefully.
Error name | Overview |
---|---|
NameError | Occurs when using undefined local variables or constants |
NoMethodError | Occurs when trying to call a method that does not exist |
SyntaxError | Occurs when there is a syntax error in the source code |
TypeError | Occurs when an object that is not the expected type or does not have the expected behavior is passed as a method argument |
SystemStackError | Occurs when the system stack overflows. Typically, it happens when you make a method call infinitely recursion. |
LoadError | Occurs when require or load fails |
For other errors, see the Official Reference (https://docs.ruby-lang.org/en/2.7.0/library/_builtin.html).
As a beginner, seeing a lot of error messages can be quite discouraging. You need to be calm and solid in responding to error messages. In case you encounter an error, you may want to think about what to do in advance.
Recommended Posts