SyntaxError
RubyFATALCommonSyntax

Ruby code has invalid syntax

Quick Answer

Check the reported line and surrounding lines for missing end, do, def, or mismatched brackets.

What this means

Raised when the Ruby parser encounters code that violates the language grammar. The program cannot be loaded or executed at all. Subclass of ScriptError.

Why it happens
  1. 1Missing end keyword to close a def, class, if, or do block
  2. 2Mismatched parentheses, brackets, or braces
  3. 3Invalid use of Ruby keywords or operators

Fix

Use ruby -c to check syntax without running

Use ruby -c to check syntax without running
# Terminal
ruby -c my_script.rb
# Syntax OK — or prints the syntax error location

Why this works

ruby -c parses the file and reports syntax errors without executing any code.

Code examples
Missing end keywordruby
def greet(name)
  puts "Hello, #{name}"
# SyntaxError: unexpected end-of-input, expecting 'end'
Mismatched bracketsruby
arr = [1, 2, 3
# SyntaxError: unexpected end-of-input
Detecting at load timeruby
begin
  eval("def bad
  puts 'oops'")   # missing end
rescue SyntaxError => e
  puts "Syntax problem: #{e.message}"
end
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors