Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 976
Andrew Grimm Avatar asked May 06 '16 08:05

Andrew Grimm


1 Answers

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

like image 98
Meier Avatar answered Oct 22 '22 12:10

Meier