0

I'm trying to mock up a hash that is dynamically generated with only 1 or 2 known key names at the top-level, so I can test with it in anticipation for converting it to a nested JSON. This is what I'm using:

ruby.exe -v

ruby 2.2.5p319 (2016-04-26 revision 54774) [i386-mingw32]

I'm having parsing issues though, and I'm not sure why, or what exactly I'm doing wrong. I really don't know what my limitations are, but this seems like a basic formatting issue. However, I've been blind to it for the last 10 mins, so maybe there's some underlying issue? Here's my whole app so far:

tcm = { \
        :tc => \
        { \
            :tests => \
            { \
                :alpha_tests => \
                { \
                    :passing_tests => {}, \
                    :failing_tests => \
                    { \
                        :alpha_test_1 => {:name => "alpha_test_1", :result => "FAILED"}, \
                        :alpha_test_2 => {:name => "alpha_test_2", :result => "FAILED"} \
                    } \
                }, \
                :beta_tests => \
                { \
                    :passing_tests => \
                    { \
                        :beta_test_1 => {:name => "beta_test_1", :result => "PASSED"} \
                    }, \
                    :failing_tests => \
                    { \
                        :beta_test_2 => {:name => "beta_test_2", :result => "FAILED"} \
                    } \
                } \
            }, \
            { \
                :test_session => 1293823 \
            } \
        } \
    }

puts tcm
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80

2 Answers2

2

The problem as I see that you missed one key:

{ \
  :test_session => 1293823 \
} \

should be

:some_key => { \
  :test_session => 1293823 \
} \

Also It's weird formatting. Better is

tcm = {
  tc: {
    tests: {
      alpha_tests: {
        passing_tests: {},
        failing_tests: {
          alpha_test_1: { name: 'alpha_test_1', result: 'FAILED' },
          alpha_test_2: { name: 'alpha_test_2', result: 'FAILED' }
        }
      },
      beta_tests: {
        passing_tests: {
          beta_test_1: { name: 'beta_test_1', result: 'PASSED' }
        },
        failing_tests: {
          beta_test_2: { name: 'beta_test_2', result: 'FAILED' }
        }
      }
    },
    key_to_fix: {
      test_session: 1_293_823
    }
  }
}

puts tcm
idej
  • 5,034
  • 1
  • 11
  • 14
1

If you delete the alpha_tests and beta_tests sections your hash is something like this:

tcm = { \
        :tc => \
        { \
            :tests => \
            { \
            }, \
            { \
                :test_session => 1293823 \
            } \
        } \
    }

In this case :tests => {}, {} is not a valid Ruby hash. You could use an array :tests => [ {}, {} ]

BTW, All those backslashes are not necessary you can also write this more succintly using the new ruby hash style ({test_session: 1293823} rather than {:test_session => 129823}) so your code might look like:

tcm = {
        tc: { 
              tests: [ 
                       { ... },
                       {
                          test_session: 1293823
                       }
                     ]
            }   
      }
Marc Rohloff
  • 1,332
  • 7
  • 8