title: Loading YAML with line number for each key
body: <p>Let's say I have a YAML file looking like this:</p>
  
  <pre>
    en:
      errors:
        # Some comment
        format: "%{attribute} %{message}"
  
        # One more comment
        messages:
          "1": "Message 1"
          "2": "Message 2"
  
      long_error_message: |
        This is a
        multiline message
  
      date:
        format: "YYYY-MM-DD"
  </pre>
  
  <p>How can I read this into a Ruby <code>Hash</code> like this?</p>
  
  <pre><code>{
    'en': {
      'errors': {
        'format': { value: '%{attribute} %{message}', line: 4 }
        'messages': {
          '1': { value: 'Message 1', line: 8 },
          '2': { value: 'Message 2', line: 9 }
        }
        'long_error_message' : { value: "This is a\nmultiline message", line: 11 }
      },
      'date': {
        'format': { value: 'YYYY-MM-DD', line: 16 }
      }
    }
  }
  </code></pre>
  
  <p>I've tried using the tip mentioned in <a href="https://www.ruby-forum.com/topic/4416285" rel="noreferrer">YAML: Find line number of key?</a> as a starting point and implemented a <code>Psych::Handler</code>, but it felt like I had to rewrite lots of code from Psych in order to get this to work.</p>
  
  <p>Any ideas how I can solve this?</p>