Ruby on rails Ruby查找并替换文件中出现的每个字符串

Ruby on rails Ruby查找并替换文件中出现的每个字符串,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想知道用Ruby实现这一点的最佳方法。我想要的是使用查询:[……]在文件中每次找到后包装所有内容,并保存文件 示例文本文件为: def ronny ronny :create, id: 1, location: nil end def alex alex :delete, id: 1, location: nil end def brandy brandy :update, id: 1, location: 555.32 end def tyson tyson :read

我想知道用Ruby实现这一点的最佳方法。我想要的是使用
查询:[……]
在文件中每次找到后包装所有内容,并保存文件

示例文本文件为:

def ronny
  ronny :create, id: 1, location: nil
end

def alex
  alex :delete, id: 1, location: nil
end

def brandy
  brandy :update, id: 1, location: 555.32
end

def tyson
  tyson :read, id: 1, location: nil
end

def sonap
  sonap :delete, id: 1, location: 31.23
end
ronny :create, query: [ id: 1, location: nil ]
一个例子是:

def ronny
  ronny :create, id: 1, location: nil
end

def alex
  alex :delete, id: 1, location: nil
end

def brandy
  brandy :update, id: 1, location: 555.32
end

def tyson
  tyson :read, id: 1, location: nil
end

def sonap
  sonap :delete, id: 1, location: 31.23
end
ronny :create, query: [ id: 1, location: nil ]

预期保存的文本文件结果应如下所示:

def ronny 
  ronny :create, query: [ id: 1, location: nil ]
end

def alex 
  alex :delete, query: [ id: 1, location: nil ]
end

def brandy 
  brandy :update, query: [ id: 1, location: 555.32 ]
end

def tyson 
  tyson :read, query: [ id: 1, location: nil ]
end

def sonap 
  sonap :delete, query: [ id: 1, location: 31.23 ]
end

有什么想法吗?

name:action,
之后的所有内容包装到
query:[]
中,尽管参数名称:

.gsub(/(id: \d+, location: [\d|\.|nil]+)/){|m| "query: [#{m}]"}
#=> "ronny :create, query: [id: 1, location: nil]"
input.gsub(/(?<=,)((\s*\w+:\s*\w+,?)+)/) do |m|
  " query: [#{m.strip}]"
end

input.gsub(/(?如果你有5个,就用手来做。否则,你需要学习一些关于正则表达式的知识。当然可以用手来做。但是我很好奇如何用Ruby来做!这很好。不幸的是,我有一个更大的文件,
location
不一定是最后一个参数。这个
gsub
看起来像什么e如果它只是查找行的末尾以放置最后一个括号,而不是
位置