Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're stuck using 1.8.7 for whatever reason (like I am), I've resorted to using <code>active_support/ordered_hash</code>. I know <code>activesupport</code> seems like a big include, but they've refactored it in later versions to where you pretty much only require the part you need in the file and the rest gets left out. Just <code>gem install activesupport</code>, and include it as shown below. Also, in your YAML file, be sure to use an !!omap declaration (and an array of Hashes). Example time!</p> <pre><code># config.yml # months: !!omap - january: enero - february: febrero - march: marzo - april: abril - may: mayo </code></pre> <p>Here's what the Ruby behind it looks like.</p> <pre><code># loader.rb # require 'yaml' require 'active_support/ordered_hash' # Load up the file into a Hash config = File.open('config.yml','r') { |f| YAML::load f } # So long as you specified an !!omap, this is actually a # YAML::PrivateClass, an array of Hashes puts config['months'].class # Parse through its value attribute, stick results in an OrderedHash, # and reassign it to our hash ordered = ActiveSupport::OrderedHash.new config['months'].value.each { |m| ordered[m.keys.first] = m.values.first } config['months'] = ordered </code></pre> <p>I'm looking for a solution that allows me to recursively dig through a <code>Hash</code> loaded from a <code>.yml</code> file, look for those <code>YAML::PrivateClass</code> objects, and convert them into an <code>ActiveSupport::OrderedHash</code>. I may post a question on that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload