Syntax Errors in Ruby - dummies

By Christopher Haupt

Ruby tries to be helpful when some part of your program has a typo or does something unexpected. Ruby displays a warning or error message, often with information about where it found the problem in your code.

For instance, if you accidentally made a typo when doing some simple math, Ruby would tell you that you have a syntax error.

Syntax is like grammar in English. A programming language’s syntax is the structure, order, and spelling of commands and statements in that language.

If you make an intentional spelling mistake:

2.2.2 :036 > 3j + 3
SyntaxError: (irb):36: syntax error, unexpected tIDENTIFIER, expecting end-of-input
3j + 3
^
from /usr/bin/irb:11:in '<main>'

You didn’t mean to type the letter j after the number 3, and unlike what you may write in school when learning algebra, this syntax is not valid Ruby.

Ruby displays an error message that’s a little cryptic, but if you see syntax error and a line number or location, it gives you a place to start investigating what went wrong. In this case, Ruby helpfully displayed the incorrect line with a little arrow symbol pointing at the point it thought was wrong. Thank you, Ruby!

What happens when you try to get Ruby to display a googolplex? A googol is a fun term that means 10 raised to the 100th power. A googolplex is 1 followed by googol (10100) zeroes. Supposedly, American mathematician Edward Kasner’s 9-year-old nephew, Milton Sirotta, coined the term and defined it to be the digit “one, followed by writing zeroes until you get tired.”

In Interactive Ruby (IRB), store a googol in a variable called googol:

2.2.2 :030 > googol = 10**100
=> 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Now try raising 10 by a googol:

2.2.2 :031 > 10**googol
(irb):31: warning: in a**b, b may be too big
=> Infinity

There are some limits to Ruby’s math skills. Here, Ruby is showing you a warning that the command you just typed isn’t working because part of the calculation is too big. It shows the results as Infinity, which seems about right.

See if Ruby can find another problem:

2.2.2 :037 > x + 5
NameError: undefined local variable or method 'x' for main:Object
from (irb):37
from /usr/bin/irb:11:in '<main>'

In this case, you tried to use a variable that you haven’t stored anything in. Ruby doesn’t know what to do here, because it can’t find a variable named x (yet). You often see this error if you make a typo in the name of a variable (or method). Check your spelling and try again.

Another common Ruby error can be seen with this code:

2.2.2 :038 > x = nil
=> nil
2.2.2 :039 > x + 5
NoMethodError: undefined method '+' for nil:NilClass
from (irb):39
from /usr/bin/irb:11:in '<main>'

You can think of nil as Ruby’s way of representing “nothing.” The error Ruby is showing means that it doesn’t know how to do addition with nil, which seems reasonable. In your code, this probably would mean you expected to receive results from some other part of the program, but the code returned nothing.

One last problem you occasionally see is if you try to do something with incompatible data:

2.2.2 :040 > x = "a"
=> "a"
2.2.2 :041 > x + 5
TypeError: no implicit conversion of Fixnum into String
from (irb):41:in '+'
from (irb):41
from /usr/bin/irb:11:in '<main>'

The letter a is assigned to variable x. Here, though, you tried to add the number 5 to the letter a. Clearly this is nonsense. Ruby thinks so, too, and tells you that it can’t convert the data in a way to make it work.

You’ll probably run into syntax errors the most often, because typos are the easiest bugs to make. When you see an error message, your best course of action is to carefully compare what you typed with your project’s code.