title: How to safe load a YAML file that includes multiple documents?
body: <p>The regular way to safe load a typical single document YAML file is done by using <code>YAML.safe_load(content)</code>.</p>
  
  <p>YAML files can contain multiple documents:</p>
  
  <pre><code>---
  key: value
  ---
  key: !ruby/struct
    foo: bar
  </code></pre>
  
  <p>Loading a YAML file such as this using <code>YAML.safe_load(content)</code> will only return the first document:</p>
  
  
  
  <pre class="lang-ruby prettyprint-override"><code>{ 'key' =&gt; 'value' }
  </code></pre>
  
  <p>If you split the file and try to safe_load the second document, you will get an exception as expected:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>Psych::DisallowedClass (Tried to load unspecified class: Struct)
  </code></pre>
  
  <p>To load multiple documents you can use <code>YAML.load_stream(content)</code> which returns an array:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>[
    { 'key' =&gt; 'value' },
    { 'key' =&gt; #&lt;struct foo="bar"&gt; }
  ]
  </code></pre>
  
  <p>The problem is that there is no <code>YAML.safe_load_stream</code> that would raise exceptions for non-whitelisted data types.</p>