0

I have a coffee script file with the following data in it. I want to create a string array that will store the following data as key:value pair in it.

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"
      ]
    sabt:
      version: 1
      groups: [
        ratio:
          default: 1
          us: 0.90
        value: "default"
      ,
        ratio:
          default: 0
          us: 0.05
        value: "colorBoost"
      ,
        ratio:
          default: 0
          us: 0.05
        value: "colorPriority"
      ,
        ratio:
          default: 0
          us: 0
        value: "noColorClause"
      ]

I want to create a String Array with these data in the following format

productRanking:LessPopularityEPC
productRanking:CtrEpcJob
productRanking:RandomPerVisitor
sabt:default
sabt:colorboost
sabt:colorPriority
sabt:nocolorClause

Is there any way to solve this problem??

3
  • 1
    Instead of trying to parse CoffeeScript in Ruby, why don't you do it in CoffeeScript? Or in Node.js with one of the CSON modules? Commented Feb 20, 2016 at 1:08
  • @Jordan I have to generate all possible combinations of key:value pair in order to automate these tests. I need to get the solution in Ruby only. Thanks for your input. Commented Feb 20, 2016 at 1:14
  • Why do you need a Ruby-only solution? If you're working in Rails then you almost certainly have access to a JavaScript interpreter so why not use it? Correctly parsing CSON is probably harder than you think. Commented Feb 20, 2016 at 1:42

1 Answer 1

1

If by String Array you mean this

['productRanking:LessPopularityEPC', 'productRanking:CtrEpcJob', 'productRanking:RandomPerVisitor']

You can do that with the following coffeescript code

data = abTests:
  ...

array = []
for testName,tests of data['abTests']
  for categoryName,categoryElems of tests['groups']
    array.push (testName + ':' + categoryElems['value'])

console.log array
#=> ['productRanking:LessPopularityEPC', 'productRanking:CtrEpcJob', 'productRanking:RandomPerVisitor']
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your solution. Appreciate it. But, I have multiple keys like that. I meant to say my data further scales like this productRanking:LessPopularityEPC productRanking:CtrEpcJob productRanking:RandomPerVisitor sabt:helloworld sabt:goworld mouseenter:true mouseenter:false Is there any way that will fetch the key ?
I have added further data from the file & the solution that I required. It would be great if you could help me with that
Could you please provide me Ruby solution ?
Could you please help me with this solution? stackoverflow.com/questions/35565007/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.