Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 用它代替红宝石一衬;端块_Ruby_Regex_Awk_Gsub - Fatal编程技术网

Ruby 用它代替红宝石一衬;端块

Ruby 用它代替红宝石一衬;端块,ruby,regex,awk,gsub,Ruby,Regex,Awk,Gsub,我想知道如何在我的所有rspec文件中替换如下代码: describe ApiController do context 'article list exist' do #... it { should respond_with :success } it { should render_template 'refresh_article_in_article_list' } end end 到 我能够用vim宏替换其中一个,但在多行中没有成功 在

我想知道如何在我的所有rspec文件中替换如下代码:

describe ApiController do
  context 'article list exist' do
      #...
      it { should respond_with :success }

      it { should render_template 'refresh_article_in_article_list' }
  end
end

我能够用vim宏替换其中一个,但在多行中没有成功

在ruby gsub的帮助下,我尝试在ruby gsub中执行此操作,但失败了,我将继续搜索:

"it { should respond_with :success }\n\nit { should render_template 'refresh_article' }".gsub(/(?<value>{.*})|(it {)|( })/, 'it do \k<value>\nend'))
=> "it do \\nend should respond_with :successit do \\nend\n\nit do \\nend should render_template 'refresh_article'it do \\nend"
“it{应该用:success}\n\nit{应该呈现模板'refresh\u article'}”。gsub(/(?{.*})|(it{)|(})/,'it do\k\nend'))
=>“it do\\nend应以以下方式响应:successit do\\nend\n\nit do\\nend应呈现模板‘刷新文章’it do\\nend”

不知道它是干什么的?添加一些“打印”以查看字段和/或变量的设置,并阅读Arnold Robbins的《有效Awk编程》第四版,如果您有任何问题,请发布具体问题。

感谢@ed morton的回答。我已经更新了这个问题以了解更多的上下文。我尝试过但没有成功,我错了。它就像一个符咒。我测试了一个大文件。下次一定会学到更多。你有多少?也许只是用手把它们换了,然后就忘了吧?@sergio tulentsev我大概有15000多行这样的代码
"it { should respond_with :success }\n\nit { should render_template 'refresh_article' }".gsub(/(?<value>{.*})|(it {)|( })/, 'it do \k<value>\nend'))
=> "it do \\nend should respond_with :successit do \\nend\n\nit do \\nend should render_template 'refresh_article'it do \\nend"
$ cat tst.awk
NF {
    prevSpaces = spaces
    spaces = $0
    sub(/[^[:space:]].*/,"",spaces)
}
!inBlock && /it *{.*}/ {
    print spaces "it do"
    inBlock = 1
}
inBlock {
    if ( !NF ) {
        print
    }
    else if ( gsub(/.*it *{ *| *} */,"") ) {
        print spaces "    " $0
    }
    else {
        print prevSpaces "end"
        inBlock = 0
    }
}
!inBlock
$
$ awk -f tst.awk file
describe ApiController do
  context 'article list exist' do
      #...
      it do
          should respond_with :success

          should render_template 'refresh_article_in_article_list'
      end
  end
end