内容数组的一行代码,用于转换 ruby 中的值字典

One line code for array of content to convert dictionary of values in ruby

data =  [ "         64               40               64              41   144      144\r\n", "         68               40               64              41   144      144\r\n", "         72               41               64              41   144      144\r\n", "         76               41               64              41   144      144\r\n", "        128               41               64              41   144      144\r\n", "        132               41               64              42   144      144\r\n", "        136               41               64              42   144      144\r\n", "        140               41               64              42   144      144\r\n"]
re = /\s+(\d+)\s+(\d+)/
dlm = []
data.each do |arr|
    val = arr.scan(re).flatten.reject(&:empty?).map(&:to_i)
    unless val.empty?
      d = {}
      d["x"] = val[0]
      d["y"] = val[1]
      d["z"] = val[2]
      d["m"] = val[3]
      d["n"] = val[4]
      d["o"] = val[5]
      dlm.append(d)
     end
end

我需要将上述代码与 ruby 放在一行中。有人可以帮忙吗?

Ruby 不需要换行。它们始终是可选的,并且始终可以替换为空格、表达式分隔符或关键字。

这意味着你可以总是任何 Ruby程序只需删除换行符:

data =  [ "         64               40               64              41   144      144\r\n", "         68               40               64              41   144      144\r\n", "         72               41               64              41   144      144\r\n", "         76               41               64              41   144      144\r\n", "        128               41               64              41   144      144\r\n", "        132               41               64              42   144      144\r\n", "        136               41               64              42   144      144\r\n", "        140               41               64              42   144      144\r\n"]; re = /\s+(\d+)\s+(\d+)/; dlm = []; data.each do |arr| val = arr.scan(re).flatten.reject(&:empty?).map(&:to_i); unless val.empty? then d = {}; d["x"] = val[0]; d["y"] = val[1]; d["z"] = val[2]; d["m"] = val[3]; d["n"] = val[4]; d["o"] = val[5]; dlm.append(d) end end

给你。一行代码。

你可以这样写。

keys = ["x", "y", "m", "n", "o"]
data.map { |s| keys.zip(s.scan(/\d+/).map(&:to_i)).to_h }
  #=> [{"x"=>64,  "y"=>40, "m"=>64, "n"=>41, "o"=>144},
  #    {"x"=>68,  "y"=>40, "m"=>64, "n"=>41, "o"=>144},
  #    {"x"=>72,  "y"=>41, "m"=>64, "n"=>41, "o"=>144},
  #    {"x"=>76,  "y"=>41, "m"=>64, "n"=>41, "o"=>144},
  #    {"x"=>128, "y"=>41, "m"=>64, "n"=>41, "o"=>144},
  #    {"x"=>132, "y"=>41, "m"=>64, "n"=>42, "o"=>144},
  #    {"x"=>136, "y"=>41, "m"=>64, "n"=>42, "o"=>144},
  #    {"x"=>140, "y"=>41, "m"=>64, "n"=>42, "o"=>144}]

您当然可以通过在第二行中替换变量 keys 来删除定义 keys 的行。

以下是 data 的前两个元素被 map 传递给块的步骤:

s = data[0]
  #=> "      64            40            64           41   144   144\r\n"
a = s.scan(/\d+/)
  #=> ["64", "40", "64", "41", "144", "144"]
b = a.map(&:to_i)
  #=> [64, 40, 64, 41, 144, 144]
c = keys.zip(b)
  #=> [["x", 64], ["y", 40], ["m", 64], ["n", 41], ["o", 144]]
c.to_h
  #=> {"x"=>64, "y"=>40, "m"=>64, "n"=>41, "o"=>144}
s = data[1]
  #=> "      68            40            64           41   144   144\r\n"
a = s.scan(/\d+/)
  #=> ["68", "40", "64", "41", "144", "144"]
b = a.map(&:to_i)
  #=> [68, 40, 64, 41, 144, 144]
c = keys.zip(b)
  #=> [["x", 68], ["y", 40], ["m", 64], ["n", 41], ["o", 144]]
c.to_h
  #=> {"x"=>68, "y"=>40, "m"=>64, "n"=>41, "o"=>144}