Andrew Grimm
Andrew Grimm

Reputation: 81510

Why isn't this Ruby syntax error detected?

The following code has invalid syntax:

# bad_code.rb
def foo
  next
end

$ ruby bad_code.rb 
bad_code.rb:2: Invalid next
bad_code.rb: compile error (SyntaxError)

However, running ruby -c bad_code.rb doesn't detect any problems:

$ ruby -c bad_code.rb 
Syntax OK

Yet ruby -c is supposed to be for checking syntax:

-c check syntax only

Why isn't it detecting this error?

rubocop bad_code.rb also doesn't detect this:

$ rubocop bad_code.rb 
Inspecting 1 file
.

1 file inspected, no offenses detected

I ran this on Ruby 2.1.10.

Upvotes: 5

Views: 208

Answers (1)

Meier
Meier

Reputation: 3880

It is syntactically correct, but when the ruby interpreter tries to produce code for it, it recognizes that there is no target for the next

Upvotes: 5

Related Questions