用它替换 ruby 一个衬垫;端块
Replace ruby one liner with it do ; end block
我想知道如何在我的所有 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
到
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
我可以用 vim 宏替换一个,但用多行替换就不行了。
在 this post 的帮助下,我尝试在 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"
$ cat tst.awk
NF {
prevSpaces = spaces
spaces = [=10=]
sub(/[^[:space:]].*/,"",spaces)
}
!inBlock && /it *{.*}/ {
print spaces "it do"
inBlock = 1
}
inBlock {
if ( !NF ) {
print
}
else if ( gsub(/.*it *{ *| *} */,"") ) {
print spaces " " [=10=]
}
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
不确定它的作用?添加一些 "prints" 以查看字段 and/or 变量设置的内容,并阅读 Arnold Robbins 着的 Effective Awk Programming,第 4 版和 post 具体问题(如果有任何问题)。
我想知道如何在我的所有 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
到
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
我可以用 vim 宏替换一个,但用多行替换就不行了。
在 this post 的帮助下,我尝试在 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"
$ cat tst.awk
NF {
prevSpaces = spaces
spaces = [=10=]
sub(/[^[:space:]].*/,"",spaces)
}
!inBlock && /it *{.*}/ {
print spaces "it do"
inBlock = 1
}
inBlock {
if ( !NF ) {
print
}
else if ( gsub(/.*it *{ *| *} */,"") ) {
print spaces " " [=10=]
}
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
不确定它的作用?添加一些 "prints" 以查看字段 and/or 变量设置的内容,并阅读 Arnold Robbins 着的 Effective Awk Programming,第 4 版和 post 具体问题(如果有任何问题)。