17

I use following now to check syntax error:

system "ruby -wc path/to/file.rb"

but it very waste time if there are too many file(for instance, i refactory code), so my question is: is there way to ruby syntax check in ruby code?

5
  • Seems like you're doing it the right way. Might be interesting for you: github.com/cypher/git-ruby-syntax-check/blob/master/pre-commit Commented Dec 3, 2014 at 13:08
  • check syntax in runtime? Commented Dec 3, 2014 at 13:09
  • where do you put that line? Commented Dec 3, 2014 at 13:10
  • 1
    OP seems to be looking for a means to check from within a Ruby script rather than the shell. But OP found the answer already, and is hoping there's some kind of better way. Commented Dec 3, 2014 at 13:10
  • Did my answer help you at all? If so, can you upvote it? Or if not, then I'll delete it. Commented Dec 9, 2014 at 20:51

4 Answers 4

8

Under MRI, you can use RubyVM::InstructionSequence#compile (relevant documentation) to compile Ruby code (which will throw exceptions if there are errors):

2.1.0 :001 > RubyVM::InstructionSequence.compile "a = 1 + 2"
 => <RubyVM::InstructionSequence:<compiled>@<compiled>>

2.1.0 :002 > RubyVM::InstructionSequence.compile "a = 1 + "
<compiled>:1: syntax error, unexpected end-of-input
a = 1 +
        ^
SyntaxError: compile error
        from (irb):2:in `compile'
        from (irb):2
        from /usr/local/rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'
Sign up to request clarification or add additional context in comments.

Comments

5

My experience has been that the easiest way to check whether my code is properly compiled is to run automated tests. The compiler is going to do the same work whether it's compiling to run tests or just checking that files are lexically correct.

MRI

MRI's parser is written in C. I couldn't find a specific reference to how to access it, though I'm sure there's a way to do that. If only someone had spent some time making Ruby more Ruby-aware...

Rubinius

In Rubinius, there is direct access to the parser via Melbourne:

rbx-2.2.10 :039 > Rubinius::ToolSets::Runtime::Melbourne.parse_file("./todo.txt")
SyntaxError: expecting keyword_do or '{' or '(': ./todo.txt:2:17

and for a valid ruby file:

rbx-2.2.10 :044 > Rubinius::ToolSets::Runtime::Melbourne.parse_file('./valid.rb')
=> #<Rubinius::ToolSets::Runtime::AST::Class:0x1e6b4 @name=#    <Rubinius::ToolSets::Runtime::AST::ClassName:0x1e6b8 @name=:RubyStuff @line=1 @superclass=#<Rubinius::ToolSets::Runtime::AST::NilLiteral:0x1e6bc @line=1>> @body=#<Rubinius::ToolSets::Runtime::AST::EmptyBody:0x1e6c8 @line=1> @line=1 @superclass=#<Rubinius::ToolSets::Runtime::AST::NilLiteral:0x1e6bc @line=1>>

Command-Line

You're currently using command-line tools to parse ruby. If you're doing some looping through files in Ruby, maybe you should just take that to the command-line as well and do something like:

jw@logopolis:/projects/open/compile-test$ find . | grep ".rb$" | xargs ruby -c
Syntax OK

jw@logopolis:/projects/open/compile-test$ find . | grep ".rb$" | xargs ruby -c
./invalid.rb:2: expecting $end: ./invalid.rb:2:3

which looks like this in Ruby:

 system "find . | grep ".rb$" | xargs ruby -c"

References

http://rubini.us/doc/en/bytecode-compiler/parser/

1 Comment

My ruby only will check one file at a time... Is there really a version of the ruby command that will take more than one file and not treat all but the first as a parameter to the first?
4

The simplest way is with the command line -c flag:

ruby -c file_you_want_to_check.rb

Comments

0

You can use Ripper, the Ruby interface to the Ruby parser:

http://ruby-doc.org/stdlib/libdoc/ripper/rdoc/Ripper.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.