I want to capture the total number of rubocop offenses to determine whether my codebase is getting better or worse. I have almost no experience with ruby scripting.
我想要捕获rubocop犯罪的总数来确定我的代码库是变好还是变坏。我几乎没有使用ruby脚本的经验。
This is my script so far:
这是我目前的剧本:
#!/usr/bin/env ruby
#script/code_coverage
var = `rubocop ../ -f fuubar -o ../coverage/rubocop_results.txt -f offenses`
puts var
So I ./rails-app/script/code_coverage
and my terminal displays
所以I ./rails-app/script/code_coverage和我的终端显示
...
--
6844 Total
When I var.inspect
I get a long string. I want to either read the output of rubocop ../ -f ...
line by line (preferred) or capture each line of output into an array.
当我检查时,我得到一个长字符串。2 .我想看一看rubocop的输出/ - f……逐行(首选)或将每一行输出捕获到一个数组中。
I would like to be able to do something like this:
我想做这样的事情:
var.each |line| do
if line.contains('total')
...
total = ...
end
Is this possible? I guess this would be similar to writing the output to a file and and then reading the file in line by line.
这是可能的吗?我猜这类似于将输出写到文件中,然后逐行读取文件。
2 个解决方案
#1
4
If you want to keep it simple, you can simply split your var
string.
如果您想保持简单,您可以简单地分割您的var字符串。
var = `rubocop ../ -f fuubar -o ../coverage/rubocop_results.txt -f offenses`.split("\n")
Then you can iterate on var
like you wanted to.
然后您可以像您希望的那样对var进行迭代。
#2
2
use open3 library of ruby. Open3 grants you access to stdin, stdout, stderr and a thread to wait the child process when running another program. You can specify various attributes, redirections, current directory, etc., of the program as Process.spawn.
使用ruby的open3库。Open3允许您访问stdin、stdout、stderr和线程,以便在运行另一个程序时等待子进程。您可以将程序的各种属性、重定向、当前目录等指定为Process.spawn。
http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html
http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html
require 'open3'
# Run lynx on this file.
cmd = "lynx -crawl -dump /data/feed/#{file_name}.html > /data/feed/#{file_name}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
cmdout = stdout.read
$logger.debug "stdout is:" + stdout.read
$logger.debug "stderr is:" + stderr.read
end
#1
4
If you want to keep it simple, you can simply split your var
string.
如果您想保持简单,您可以简单地分割您的var字符串。
var = `rubocop ../ -f fuubar -o ../coverage/rubocop_results.txt -f offenses`.split("\n")
Then you can iterate on var
like you wanted to.
然后您可以像您希望的那样对var进行迭代。
#2
2
use open3 library of ruby. Open3 grants you access to stdin, stdout, stderr and a thread to wait the child process when running another program. You can specify various attributes, redirections, current directory, etc., of the program as Process.spawn.
使用ruby的open3库。Open3允许您访问stdin、stdout、stderr和线程,以便在运行另一个程序时等待子进程。您可以将程序的各种属性、重定向、当前目录等指定为Process.spawn。
http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html
http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html
require 'open3'
# Run lynx on this file.
cmd = "lynx -crawl -dump /data/feed/#{file_name}.html > /data/feed/#{file_name}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
cmdout = stdout.read
$logger.debug "stdout is:" + stdout.read
$logger.debug "stderr is:" + stderr.read
end