I have problems to check syntax of ruby scripts that has rails script/runner on its shebang.
我在检查rubb脚本的语法时遇到问题,这些脚本在其shebang上有rails script / runner。
Here are two example scripts and how they responses to ruby syntax checking:
以下是两个示例脚本以及它们如何响应ruby语法检查:
Script hello_world_runner.rb
:
脚本hello_world_runner.rb:
#!/usr/bin/env script/runner
p "Hello world!"
Script hello_world.rb
脚本hello_world.rb
#!/usr/bin/env ruby
p "Hello world!"
Here is how I try to check the syntax. First line is a command and a second line is an output.
以下是我尝试检查语法的方法。第一行是命令,第二行是输出。
$ ruby -c hello_world_runner.rb
"Hello world!"
$ ruby -c hello_world.rb
SYNTAX OK
5 个解决方案
#1
12
You can try this
你可以试试这个
tail -n +2 hellow_world_runner.rb | ruby -c
Not ideal but should work.
不理想,但应该工作。
#2
5
You can rewrite your script like this:
您可以像这样重写脚本:
Rails 2:
Rails 2:
#!/usr/bin/env ruby
require File.expand_path(Dir.pwd + '/config/boot', __FILE__)
require RAILS_ROOT + '/config/environment'
p "Hello world!"
Rails 3:
Rails 3:
#!/usr/bin/env ruby
require File.expand_path(Dir.pwd + '/config/boot', __FILE__)
require File.expand_path(Dir.pwd + '/config/application', __FILE__)
Rails.application.require_environment!
p "Hello world!"
Of course, you need to define your own (absolute) paths or run this script from Rails root.
当然,您需要定义自己的(绝对)路径或从Rails根目录运行此脚本。
$ ruby -c ./test.rb
Syntax OK
#3
2
I would highly recommend that you factor the majority of the code in those scripts into your core domain models/lib directory/etc. This will allow you to test the script logic the same way you test the rest of your application (which will also end up checking syntax) and reduce the actual content of your executable file to a line or two.
我强烈建议您将这些脚本中的大部分代码考虑到核心域模型/ lib目录/ etc中。这将允许您以与测试应用程序其余部分相同的方式测试脚本逻辑(这也将最终检查语法)并将可执行文件的实际内容减少到一行或两行。
#4
2
The ruby command line has these 2 flags that might assist you, I use:
ruby命令行有这两个可能对你有帮助的标志,我使用:
ruby -wc test.rb
#5
1
Here is a little cheat:
这是一个小骗子:
$ ruby -wc <(cat <(echo ruby) hello_world_runner.rb)
Syntax OK
Basically ruby
syntax checker expect the ruby
word in the first line (shebang).
基本上ruby语法检查器期望第一行(shebang)中的ruby单词。
#1
12
You can try this
你可以试试这个
tail -n +2 hellow_world_runner.rb | ruby -c
Not ideal but should work.
不理想,但应该工作。
#2
5
You can rewrite your script like this:
您可以像这样重写脚本:
Rails 2:
Rails 2:
#!/usr/bin/env ruby
require File.expand_path(Dir.pwd + '/config/boot', __FILE__)
require RAILS_ROOT + '/config/environment'
p "Hello world!"
Rails 3:
Rails 3:
#!/usr/bin/env ruby
require File.expand_path(Dir.pwd + '/config/boot', __FILE__)
require File.expand_path(Dir.pwd + '/config/application', __FILE__)
Rails.application.require_environment!
p "Hello world!"
Of course, you need to define your own (absolute) paths or run this script from Rails root.
当然,您需要定义自己的(绝对)路径或从Rails根目录运行此脚本。
$ ruby -c ./test.rb
Syntax OK
#3
2
I would highly recommend that you factor the majority of the code in those scripts into your core domain models/lib directory/etc. This will allow you to test the script logic the same way you test the rest of your application (which will also end up checking syntax) and reduce the actual content of your executable file to a line or two.
我强烈建议您将这些脚本中的大部分代码考虑到核心域模型/ lib目录/ etc中。这将允许您以与测试应用程序其余部分相同的方式测试脚本逻辑(这也将最终检查语法)并将可执行文件的实际内容减少到一行或两行。
#4
2
The ruby command line has these 2 flags that might assist you, I use:
ruby命令行有这两个可能对你有帮助的标志,我使用:
ruby -wc test.rb
#5
1
Here is a little cheat:
这是一个小骗子:
$ ruby -wc <(cat <(echo ruby) hello_world_runner.rb)
Syntax OK
Basically ruby
syntax checker expect the ruby
word in the first line (shebang).
基本上ruby语法检查器期望第一行(shebang)中的ruby单词。