Question: 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?

Answer: 
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/>
You can use Ripper, the Ruby interface to the Ruby parser:

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