Search code examples
rubydebuggingintellij-idearubymine

How do I interpret this debugger output?


Can anyone tell me how to get nth value, lets say 3rd val (which is 1) in children[0] in the following debugger output? I tried children[0][2] but didn't work.

children = Array (101 elements)
 [0] = Hash (1 element)
  bitstring => Array (100 elements)
   key = {Symbol} bitstring
   value = Array (100 elements)
    [0] = 1
    [1] = 0
    [2] = 1
    [3] = 1
    [4] = 1
    [5] = 1

Solution

  • You would get it via children[0][:bitstring][2], which can be easily seen from the debugger output.

    The way the debugger output is structured is as a list of variable_name = {Class} value where {Class} is the Class type of the value if it is not a primitive or a collection, in which case that part (including the braces) is left out.

    When the value is some kind of collection, the collection type and number of elements in the collection is shown where the value goes, and then beginning on the next line, and indented an extra amount, each element is listed in the form accessor = value. If that value is also a collection, then the same thing goes again: the collection type and number of elements in the collection are shown where the value goes, and then beginning on the next line, and indented an extra amount. So when you look at this:

    children = Array (101 elements)
     [0] = Hash (1 element)
      bitstring => Array (100 elements)
       key = {Symbol} bitstring
       value = Array (100 elements)
        [0] = 1
        [1] = 0
        [2] = 1
        [3] = 1
        [4] = 1
        [5] = 1
    

    You should see that

    • children is an Array
    • children[0] is a Hash
    • children[0][:bitstring] is an array
    • children[0][:bitstring][2] is the third element of the :bitstring array and its value is the integer 1

    The way you know that the Hash key is :bitstring and not "bitstring" is that for a hash, each element is itself a collection of key and value, so you see that under the line bitstring => Array (100 elements) are 2 lines, one starting with key and one starting with value. The key is listed as {Symbol} bitstring which tells you it is the symbol :bitstring. If the key were a string, it would just say key = "bitstring" (note the quotation marks).