Ruby数组:更改字符串元素格式

Mel*_*lon 0 ruby ruby-on-rails

我有一个字符串数组,其中包含" firstname.lastname "字符串:

customers = ["aaa.bbb", "ccc.ddd", "www.uuu", "iii.oooo", ...]
Run Code Online (Sandbox Code Playgroud)

现在,我想通过删除" . "并使用空格来传输数组中每个元素的字符串格式.那是我想要的数组:

customers = ["aaa bbb", "ccc ddd", "www uuu", "iii oooo", ...]
Run Code Online (Sandbox Code Playgroud)

最有效的方法是什么?

- - - - - - - - 更多 - - - - - - - - - -

而关于我的情况下,如何在这里

Pau*_*ves 7

customers.collect!{|name| name.gsub(/\./, " ")}
Run Code Online (Sandbox Code Playgroud)

更新

@tadman有,这是我的基准FWIW

require 'benchmark'

customers = []
500000.times { customers << "john.doe" }

Benchmark.bm do|b|
  b.report("+= ") do
    # customers.collect!{|name| name.gsub(/\./, " ")} # 2.414220
    # customers.each { |c| c.gsub!(/\./, ' ') } # 2.223308
    customers.each { |c| c.tr!('.', ' ') } # 0.379226
  end
end
Run Code Online (Sandbox Code Playgroud)