为什么我把它推到阵列时没有得到正确的价值?

时间:2016-02-22 22:14:49

标签: arrays ruby hash

我有以下格式的数据。

  # AB Tests
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  abTests:
    productRanking:
      version: 4
      groups: [
        ratio:
          default: 1
          us: 0.90
          me: 0.0
        value: "LessPopularityEPC"
      ,
        ratio:
          default: 0
          us: 0.1
        value: "CtrEpcJob"
      ,
        ratio:
          default: 0
          me: 1.0
        value: "RandomPerVisitor"
      ]
  # Routes

我想将以下输出作为字符串数组:

productRanking:LessPopularityEPC
productRanking:CtrEpcJob
productRanking:RandomPerVisitor

我使用以下代码将密钥和值与数据和数据分开。将它们存储到数组中

START_REGEXP = /# AB Tests/
END_REGEXP = /# Routes/
COMMENT_EXP = /#/

#Function to fetch the key:value & store them into array

def Automate_AB_tests.store_key_value(input_file)
    prev_line = ""
    curr_line = ""
    array = []
    flag = false

    IO.foreach(input_file) do |line|
      prev_line = curr_line
      curr_line = line
      flag = true if line =~ START_REGEXP

      if flag
        unless line =~ COMMENT_EXP
          if line.include? 'version: '
            key = prev_line
            puts key #productRanking: sabt:
          end

          if line.include? 'value: '
            value = line.delete('\n').delete('"').split[1] #LessPopularityEPC CtrlEpcJob RandomPerVisitor
            array << "#{key}:#{value}"
          end
        end

        flag = false if line =~ END_REGEXP
      end 
    end

    puts array
  end

它正在获取键但不将这些键存储到stringArray中。如果有人能够指出我的代码有什么问题,那么它会非常棒。我得到的输出如下:

    productRanking:
:LessPopularityEPC
:CtrEpcJob
:RadomPerVisitor

1 个答案:

答案 0 :(得分:2)

由于某些原因,你显然无法安装Node.js,这会让你真正做到这一点非常简短,你就会陷入困境。

我提出了另一个丑陋的黑客攻击:CSON与YAML并没有什么不同。做一些简单的替换,将其转换为YAML,然后在Ruby中解析它。

警告:像所有丑陋的黑客一样,这是非常脆弱的。一旦你试图度假,它可能会破裂。使用风险自负。

require "yaml"

data = <<END
  # AB Tests
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  abTests:
    productRanking:
      version: 4
      groups: [
        ratio:
          default: 1
          us: 0.90
          me: 0.0
        value: "LessPopularityEPC"
      ,
        ratio:
          default: 0
          us: 0.1
        value: "CtrEpcJob"
      ,
        ratio:
          default: 0
          me: 1.0
        value: "RandomPerVisitor"
      ]
  # Routes
END

AB_TESTS_SECTION_EXPR = /^( +)abTests:\n.+(?=^\1[^ ])/m

# Extract text between "# AB Tests" and "# Routes"
ab_tests = data[AB_TESTS_SECTION_EXPR]

# Turn the CSON array into a YAML array by removing the square brackets and
# commas and inserting a `-` before each "ratio:"
yaml = ab_tests.gsub(/\s+\[$/, '')
         .gsub(/  ratio:\s*$/, '- ratio:')
         .gsub(/^\s*[,\]]\s*$/, '')

puts yaml

这将打印以下有效的YAML:

abTests:
  productRanking:
    version: 4
    groups:
    - ratio:
        default: 1
        us: 0.90
        me: 0.0
      value: "LessPopularityEPC"

    - ratio:
        default: 0
        us: 0.1
      value: "CtrEpcJob"

    - ratio:
        default: 0
        me: 1.0
      value: "RandomPerVisitor"

现在我们只需要将YAML解析为Ruby并提取我们需要的数据:

hsh = YAML.load(yaml)

hsh["abTests"].each do |key, val|
  val["groups"].each do |group|
    puts "#{key}:#{group['value']}"
  end
end
# => productRanking:LessPopularityEPC
#    productRanking:CtrEpcJob
#    productRanking:RandomPerVisitor