如何检查 ruby 代码中的 ruby 语法错误 [英] How to check ruby syntax error in ruby code
本文介绍了如何检查 ruby 代码中的 ruby 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在使用以下来检查语法错误:
I use following now to check syntax error:
system "ruby -wc path/to/file.rb"
但是如果文件太多(例如,我重构代码)会很浪费时间,所以我的问题是: 有没有办法在 ruby 代码中检查 ruby 语法?
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?
推荐答案
在 MRI 下,您可以使用 RubyVM::InstructionSequence#compile
(相关文档) 来编译 Ruby 代码(如果有错误会抛出异常):>
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>'
这篇关于如何检查 ruby 代码中的 ruby 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文