简体   繁体   English

使用jruby -c语法检查来检查ruby / jruby脚本的语法(内部ruby代码)

[英]check syntax of ruby/jruby script using jruby -c syntax check (inside ruby code)

I am in a search of some way , using which in ruby code I should be able to create a temp file and then append some ruby code in that, then pass that temp file path to jruby -c to check for any syntax errors. 我正在寻找某种方式,使用它可以在ruby代码中创建一个临时文件,然后在其中附加一些ruby代码,然后将该临时文件路径传递给jruby -c以检查是否存在语法错误。

Currently I am trying the following approach: 目前,我正在尝试以下方法:

script_file = File.new("#{Rails.root}/test.rb", "w+") 
            script_file.print(content)
            script_file.close
command = "#{RUBY_PATH} -c #{Rails.root}/test.rb"
            eval(command);
            new_script_file.close

When I inspect command var, it is properly showing jruby -c {ruby file path}. 当我检查命令var时,它正确显示了jruby -c {ruby文件路径}。 But when I execute the above piece of code I am getting the following error: 但是,当我执行以上代码时,出现以下错误:

Completed 500 Internal Server Error in 41ms 在41毫秒内完成500个内部服务器错误

SyntaxError ((eval):1: dhunknown regexp options - dh): 语法错误((eval):1:不知道的正则表达式选项-dh):

Let me know if any one has any idea on this. 让我知道是否有人对此有任何想法。

Thanks, Dean 谢谢院长

eval evaluates the string as Ruby code , not as a command line invocation: Since your command is not valid Ruby syntax, you get this exception. eval将字符串评估为Ruby代码 ,而不是命令行调用:由于您的命令不是有效的Ruby语法,因此会出现此异常。

If you want to launch a command in Ruby, you can use %x{} or ``: 如果要在Ruby中启动命令,可以使用%x {}或``:

output1 = ls 输出1 = ls

output2 = %x{ls} output2 =%x {ls}

Both forms will return the output of the launched command as a String, if you want to process it. 如果要处理,两种形式都将以字符串形式返回启动命令的输出。 If you want this output to be directly displayed in the user terminal, you can use system(): 如果希望此输出直接显示在用户终端中,则可以使用system():

system("ls") 系统(“ ls”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM