如何将这个 ruby 字符串变成一个普通的数组?

How to turn this ruby string into a normal array?

我正在检索一个如下所示的字符串:

"[[[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]", "[]", "[]", "[]"]

map 然后将 a 的每个元素传递给它的块,并将该值分配给块变量 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)     #=> []

基准

我收到了对方法进行基准比较的请求。我很高兴满足这一要求,并在下面报告结果。 las,我的解决方案并没有那么好,但是当基准测试请求来自某个姓氏不包含元音字母("sometimes 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)