Ruby syntax errors and countermeasures

As a prime example of a memorandum, I will write down the errors I encountered and the solutions.

① The syntax is wrong? [syntax error, unexpected tIDENTIFIER]

syntax error = Since it is a syntax error, I get angry if something is wrong with the code I wrote. The code below is the result of outputting the file in the terminal.

main2.rb:81: syntax error, unexpected tIDENTIFIER, expecting ')'
      hp: params[:hp]
main2.rb:82: syntax error, unexpected ':', expecting end
      offense: params[:offense]
main2.rb:83: syntax error, unexpected ':', expecting end
      defense: params[:defense]
main2.rb:84: syntax error, unexpected ')', expecting end
    )

main2.rb(Wrong code)


super(
      name: params[:name]
      hp: params[:hp]
      offense: params[:offense]
      defense: params[:defense]
    )

main2.rb (modified code)


super(
      name: params[:name],
      hp: params[:hp],
      offense: params[:offense],
      defense: params[:defense]
    )

The cause was that each item in the super method was not separated by "," and all were treated as the same line, so I was angry.

** Note that the super method calls the method overridden by the method you are executing. Overriding is "overriding a method of a parent class with a child class". By using super in the initialize method, it is possible to execute the initialize method of the specified class. As a result, processing can be divided into "common parts" and "non-common parts". ** **

② I don't know such a name! uninitialized constant (NameError)

main.rb


Traceback (most recent call last):
main.rb:1:in `<main>': uninitialized constant <class name> (NameError)

I am angry that there is no such class name in the main.rb file. If you get an error that you want to call a separate file, Let's describe require as shown below so that another file can be called.

require './<class name>'

③ Pass the required value! ArgumentError

wrong number of arguments (given 1, expected 0) (ArgumentError)

Occurs in case of argument error. I'm passing one required value as (given 1, expected 0), but I'm angry that there is no recipient. Since there is a high possibility that you forgot to describe the argument in the first place, let's check the described method.

④ Let's define the method! NoMethodError

monster.rb:51:in `transform': undefined method `transform_message' for #<Monster:0x00007fea761c2b28> (NoMethodError)

Literally an error due to an undefined method.

It could simply be due to a typo, but it's also possible that some method forgot to close def ~ end. Pay attention to the position of the indent.

** The following will be added when an error is encountered and resolved. ** **

Recommended Posts

Ruby syntax errors and countermeasures
Ruby syntax summary
Ruby and Gem
[Ruby] Classes and instances
Symbols and Destructive Ruby
[Ruby] Big Decimal and DECIMAL
Ruby classes and instances
Ruby inheritance and delegation
Ruby variables and methods
Ruby Learning # 28 Handling Errors
Differences between Ruby syntax error statements in Ruby and binary
GraphQL Ruby and actual development
[Rails] N + 1 problems and countermeasures
About Ruby hashes and symbols
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
About Ruby and object model
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby variables and functions (methods)
Ruby methods and classes (basic)
Creating Ruby classes and instances
About self-introduction and common errors
[Ruby] Singular methods and singular classes
[Ruby] Difference between get and post
[Ruby] present/blank method and postfix if.
[Ruby] Difference between is_a? And instance_of?
Ruby standard input and various methods
About Ruby single quotes and double quotes
[Ruby] then keyword and case in
[Ruby basics] split method and to_s method
About Ruby product operator (&) and sum operator (|)
Write keys and values in Ruby
[Ruby] If and else problems-with operators-
[Ruby] Boolean values ​​and logical operators
Project ruby and rails version upgrade
About object-oriented inheritance and about yield Ruby