迭代期间 ruby 中的 Grep 命令

Grep command in ruby during iteration

可以在 shell 脚本中完成,但想在 ruby 中尝试,因为学习了,请帮助解决这个问题。

在迭代期间尝试从文件 (list) 中 grep 变量 (line) 时,没有显示输出

但是像下面这样单独做的时候,可以看到输出

Getting Output: ("Phone" is one of the lines present in the file)

    value = `grep -i "phone" list`
        puts value

 No Output:
     File.open("list") do |f|
            f.each_line do |line|
            puts line
            value = `grep -i "line" list`
    #       puts list
            puts value
            end
    end

Ruby这样的写法:

File.open('list').each_line.grep(/phone/i)

文档: