Ruby:拆分,然后删除前导/尾随空格?

Ben*_*Ben 32 ruby

things = "one thing, two things, three things, four things"
Run Code Online (Sandbox Code Playgroud)

鉴于此输入,如何用逗号分割字符串然后修剪它周围的空白?导致:

things = ["one thing", "two things", "three things", "four things"]
Run Code Online (Sandbox Code Playgroud)

目前我有这个:

things = things.to_s.tr("\n\t", "").strip.split(/,/)
Run Code Online (Sandbox Code Playgroud)

除了在逗号上拆分时删除前导/尾随空格,这除了我想要它做的大部分工作之外.实现这一目标的最佳方法是什么?我想把它作为这个表达式的一部分,而不是将上面的结果分配给一个单独的数组并迭代它.

Aru*_*hit 73

s = "one thing, two things, three things, four things"
s.split(",").map(&:strip)
# => ["one thing", "two things", "three things", "four things"]
Run Code Online (Sandbox Code Playgroud)

在我的Ubuntu 13.04操作系统中,使用Ruby 2.0.0p0

require 'benchmark'

s = "one thing, two things, three things, four things"
result = ""

Benchmark.bmbm do |b|
  b.report("strip/split: ") { 1_000_000.times {result = s.split(",").map(&:strip)} }
  b.report("regex: ") { 1_000_000.times {result = s.split(/\s*,\s*/)} }
end

Rehearsal -------------------------------------------------
strip/split:    6.260000   0.000000   6.260000 (  6.276583)
regex:          7.310000   0.000000   7.310000 (  7.320001)
--------------------------------------- total: 13.570000sec

                    user     system      total        real
strip/split:    6.350000   0.000000   6.350000 (  6.363127)
regex:          7.290000   0.000000   7.290000 (  7.302163)
Run Code Online (Sandbox Code Playgroud)

  • 完善.非常感谢! (2认同)

Kor*_*tor 7

使用正则表达式#split:

"one thing, two things, three things, four things".split /\s*,\s*/
# => ["one thing", "two things", "three things", "four things"]
Run Code Online (Sandbox Code Playgroud)


Jon*_*ern 6

作为一个速度迷,我喜欢基准...但是让我们面对它,除非你在你的代码中的一百万个循环中进行这个操作,速度差异可能不会影响你的代码几乎与明显的编码结构一样多.

在我看来,如果性能无关紧要,@ arup拥有最好,最直接,最清晰的解决方案.

  • 你的解决方案与我的完全一样..*dup*答案需要什么? (2认同)

xen*_*tek 5

不是为了打败一匹马,但你可以通过做出两个改变来加快速度,这两个改变现在已经成为我的第二天性。第一个是使用map!而不是map避免创建分割数组的副本,第二个是避免使用符号进行过程语法(例如&:split,它添加了一个额外的操作,可以使用更详细的语法来避免)。

基准如下:

require 'benchmark'

s = "one thing, two things, three things, four things"
result = ""

Benchmark.bmbm do |b|
    b.report("strip/split (map/to_proc): ") { 1_000_000.times { result = s.split(",").map(&:strip) } }
    b.report("strip/split (map): ") { 1_000_000.times { result = s.split(",").map { |e| e.strip } } }
    b.report("strip/split (map!/to_proc): ") { 1_000_000.times { result = s.split(",").map!(&:strip) } }
    b.report("strip/split (map!): ") { 1_000_000.times { result = s.split(",").map! { |e| e.strip } } }
    b.report("regex: ") { 1_000_000.times { result = s.split(/\s*,\s*/) } }
end
Run Code Online (Sandbox Code Playgroud)

结果:

                                   user     system      total        real
strip/split (map/to_proc):     5.230000   0.010000   5.240000 (  5.283079)
strip/split (map):             4.660000   0.010000   4.670000 (  4.716920)
strip/split (map!/to_proc):    4.440000   0.020000   4.460000 (  4.492943)
strip/split (map!):            4.320000   0.010000   4.330000 (  4.365386)
regex:                         7.190000   0.060000   7.250000 (  7.322932)
Run Code Online (Sandbox Code Playgroud)

请记住阅读相对于彼此的数字,而不是相对于其他答案中提供的基准的数字。