如何将这个红宝石字符串转换为普通数组

How to turn this ruby string into a normal array?

本文关键字:转换 数组 字符串 红宝石      更新时间:2023-09-26

我正在检索一个看起来像这样的字符串:

"[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], [], []] [[[256, 498]], [], [], []]"

我怎样才能让它看起来像这样?

[[256,498],[256,498],[],[256,498],[],[],[256,498],[],[],[]]

寻找 ruby 或 JavaScript 的解决方案。

仍在寻找更优雅的解决方案,但这会起作用

str.scan(/'[+('d*, ?'d*)?']+/).flatten.map do |a|
  a ? a.split(",").map(&:to_i) : []
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]

方法细目

scanned_string_array = str.scan(/'[+('d*, ?'d*)?']+/)
#=> [["256, 498"], ["256, 498"], [nil], ["256, 498"], [nil], [nil], ["256, 498"], [nil], [nil], [nil]]
scanned_string_array.flatten
#=> ["256, 498", "256, 498", nil, "256, 498", nil, nil, "256, 498", nil, nil, nil]
scanned_string_array.flatten.map do |a|
  #if a is nil return empty array otherwise split on comma and map to Integer
  a ? a.split(",").map(&:to_i) : []
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]

更新:

找到了更优雅的方式,或者至少我更喜欢它

str.scan(/'[+('d*),?'s?('d*)?']+/).map do |a|
  a.reject(&:empty?).map(&:to_i)
end
#=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []]

我将按如下方式提取数组。

法典

def extract(str)
  str.scan(/'[[^'[']]*']/).map { |s| s.scan(/'d+/).map(&:to_i) }
end

str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " '
      "[], []] [[[256, 498]], [], [], []]"
extract(str)
  #=> [[256, 498], [256, 498], [], [256, 498], [], [], [256, 498], [], [], []] 

解释

对于上面的示例:

str.scan 提取所有形式为 "[...]" 的字符串,其中...是左大括号和右大括号以外的字符:

a = str.scan(/'[[^'[']]*']/)
  #=> ["[256, 498]", "[256, 498]", "[]", "[256, 498]",
  #    "[]", "[]", "[256, 498]", "[]", "[]", "[]"]

然后mapa的每个元素传递给它的块,并将该值分配给块变量s,第一个是:

s = "[256, 498]"

然后执行以下操作:

ss = s.scan(/'d+/) #=> ["256", "498"] 
ss.map(&:to_i)     #=> [256, 498] 

"[]"传递到块中时:

s = "[]"
ss = s.scan(/'d+/) #=> []
ss.map(&:to_i)     #=> []

基准

我要求对方法进行基准比较。 我很高兴满足这一要求,并在下面报告结果。 唉,我的解决方案表现不佳,但是当对基准测试的请求来自某个姓氏不包含元音(除了"有时y")的工程师时,这种情况似乎总是会发生。

我只做了一个基准测试,但是当我改变测试数据时,结果是相似的。

方法比较

module Methods
  def smnky1(str)
    str.scan(/'[+('d*, ?'d*)?']+/).flatten.map do |a|
      a ? a.split(",").map(&:to_i) : []
    end
  end
  def smnky2(str)
    str.scan(/'[+('d*),?'s?('d*)?']+/).map do |a|
      a.reject(&:empty?).map(&:to_i)
    end
  end
  def cary(str)
    str.scan(/'[[^'[']]*']/).map { |s| s.scan(/'d+/).map(&:to_i) }
  end
end
@methods = Methods.instance_methods(false)
include Methods
def compute(m, str) send(m, str) end

基准代码

require 'benchmark'
@indent = @methods.map { |m| m.to_s.size }.max
def test(str, reps)
  exit if answers_not_all_the_same(str)
  Benchmark.bm(@indent) do |bm|
    @methods.each do |m|
      bm.report m.to_s do
        reps.times { compute(m, str) }
      end
    end
  end
end
def answers_not_all_the_same(str)
  same = @methods.map { |m| compute(m, str) }.uniq.size > 1
  puts same ? "all don't match" : "all match"
end

结果

str = "[[[256, 498]]] [[[256, 498]], []] [[[256, 498]], " '
      "[], []] [[[256, 498]], [], [], []]"
reps = 100_000
puts "Example string, #{reps} repetitions"
test(str, reps)
Example string, 100000 repetitions
all match
             user     system      total        real
smnky1   1.830000   0.000000   1.830000 (  1.830457)
smnky2   1.920000   0.010000   1.930000 (  1.920094)
cary     2.750000   0.000000   2.750000 (  2.752946)