如何检查ruby代码中的ruby语法错误

时间:2022-10-20 22:42:31

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?

但是如果文件太多(例如,我的重构代码),它会浪费时间,所以我的问题是:有没有办法在ruby代码中进行ruby语法检查?

3 个解决方案

#1


6  

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

在MRI下,你可以使用RubyVM :: InstructionSequence#compile(相关文档)来编译Ruby代码(如果有错误会抛出异常):

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>'

#2


1  

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...

MRI的解析器是用C语言编写的。我找不到具体的参考方法,但我确信有办法解决这个问题。如果只有人花了一些时间让Ruby更容易识别Ruby ......

Rubinius

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

在Rubinius,可以通过墨尔本直接访问解析器:

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:

并为有效的ruby文件:

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:

您当前正在使用命令行工具来解析ruby。如果您正在使用Ruby中的文件进行循环,也许您应该将其带到命令行并执行以下操作:

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:

在Ruby中看起来像这样:

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

References

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

#3


0  

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

您可以使用Ripper,Ruby解析器的Ruby接口:

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

#1


6  

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

在MRI下,你可以使用RubyVM :: InstructionSequence#compile(相关文档)来编译Ruby代码(如果有错误会抛出异常):

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>'

#2


1  

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...

MRI的解析器是用C语言编写的。我找不到具体的参考方法,但我确信有办法解决这个问题。如果只有人花了一些时间让Ruby更容易识别Ruby ......

Rubinius

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

在Rubinius,可以通过墨尔本直接访问解析器:

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:

并为有效的ruby文件:

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:

您当前正在使用命令行工具来解析ruby。如果您正在使用Ruby中的文件进行循环,也许您应该将其带到命令行并执行以下操作:

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:

在Ruby中看起来像这样:

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

References

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

#3


0  

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

您可以使用Ripper,Ruby解析器的Ruby接口:

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