Ruby/Rails 使用 gsub 和数组

发布于 2024-09-29 15:48:40 字数 160 浏览 5 评论 0原文

我有一个字符串,我试图在 Ruby 中使用 gsub 方法来处理它。问题是我有一个动态字符串数组,我需要迭代它来搜索原始文本并替换为。

例如,如果我有以下原始字符串(这是我正在使用的一些示例文本,希望能使其全部正常工作)并且有一组我想要搜索和替换的项目。

感谢您提前的帮助!

I have a string that I am trying to work with in using the gsub method in Ruby. The problem is that I have a dynamic array of strings that I need to iterate through to search the original text for and replace with.

For example if I have the following original string (This is some sample text that I am working with and will hopefully get it all working) and have an array of items I want to search through and replace.

Thanks for the help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淤浪 2024-10-06 15:48:40

这是您要找的吗?

ruby-1.9.2-p0 > arr = ["This is some sample text", "text file"]  
 => ["This is some sample text", "text file"] 

ruby-1.9.2-p0 > arr = arr.map {|s| s.gsub(/text/, 'document')}
 => ["This is some sample document", "document file"] 

Is this what you are looking for?

ruby-1.9.2-p0 > arr = ["This is some sample text", "text file"]  
 => ["This is some sample text", "text file"] 

ruby-1.9.2-p0 > arr = arr.map {|s| s.gsub(/text/, 'document')}
 => ["This is some sample document", "document file"] 
青朷 2024-10-06 15:48:40
a = ['This is some sample text',
     'This is some sample text',
     'This is some sample text']

所以 a 是示例数组,然后循环数组并替换值

a.each do |s|
    s.gsub!('This is some sample text', 'replacement')
end
a = ['This is some sample text',
     'This is some sample text',
     'This is some sample text']

so a is the example array, and then loop through the array and replace the value

a.each do |s|
    s.gsub!('This is some sample text', 'replacement')
end
留一抹残留的笑 2024-10-06 15:48:40

使用 Array#fill替换所有内容

irb(main):008:0> a
=> {:a=>1, :b=>2, :c=>3, :d=>nil, :e=>5}
irb(main):009:0> a.values
=> [1, 2, 3, nil, 5]
irb(main):010:0> a.values.fill(:x)
=> [:x, :x, :x, :x, :x]

仅替换匹配元素

使用 Array#map 和一个三元运算符

irb(main):008:0> a
=> {:a=>1, :b=>2, :c=>3, :d=>nil, :e=>5}
irb(main):009:0> a.values
=> [1, 2, 3, nil, 5]
irb(main):012:0> a.values.map { |x| x.nil? ? 'void' : x }
=> [1, 2, 3, "void", 5]
irb(main):016:0> a.values.map { |x| /\d/.match?(x.to_s) ? 'digit' : x }
=> ["digit", "digit", "digit", nil, "digit"]

Replace everything

Using Array#fill.

irb(main):008:0> a
=> {:a=>1, :b=>2, :c=>3, :d=>nil, :e=>5}
irb(main):009:0> a.values
=> [1, 2, 3, nil, 5]
irb(main):010:0> a.values.fill(:x)
=> [:x, :x, :x, :x, :x]

Replace only matching elements

Using Array#map and a ternary operator.

irb(main):008:0> a
=> {:a=>1, :b=>2, :c=>3, :d=>nil, :e=>5}
irb(main):009:0> a.values
=> [1, 2, 3, nil, 5]
irb(main):012:0> a.values.map { |x| x.nil? ? 'void' : x }
=> [1, 2, 3, "void", 5]
irb(main):016:0> a.values.map { |x| /\d/.match?(x.to_s) ? 'digit' : x }
=> ["digit", "digit", "digit", nil, "digit"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文