Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:44:50+00:00 2026-06-13T11:44:50+00:00

I need to open a YAML file with aliases used inside it: defaults: &defaults

  • 0

I need to open a YAML file with aliases used inside it:

defaults: &defaults
  foo: bar
  zip: button

node:
  <<: *defaults
  foo: other

This obviously expands out to an equivalent YAML document of:

defaults:
  foo: bar
  zip: button

node:
  foo: other
  zip: button

Which YAML::load reads it as.

I need to set new keys in this YAML document and then write it back out to disk, preserving the original structure as much as possible.

I have looked at YAML::Store, but this completely destroys the aliases and anchors.

Is there anything available that could something along the lines of:

thing = Thing.load("config.yml")
thing[:node][:foo] = "yet another"

Saving the document back as:

defaults: &defaults
  foo: bar
  zip: button

node:
  <<: *defaults
  foo: yet another

?

I opted to use YAML for this due to the fact it handles this aliasing well, but writing YAML that contains aliases appears to be a bit of a bleak-looking playing field in reality.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:44:51+00:00Added an answer on June 13, 2026 at 11:44 am

    The use of << to indicate an aliased mapping should be merged in to the current mapping isn’t part of the core Yaml spec, but it is part of the tag repository.

    The current Yaml library provided by Ruby – Psych – provides the dump and load methods which allow easy serialization and deserialization of Ruby objects and use the various implicit type conversion in the tag repository including << to merge hashes. It also provides tools to do more low level Yaml processing if you need it. Unfortunately it doesn’t easily allow selectively disabling or enabling specific parts of the tag repository – it’s an all or nothing affair. In particular the handling of << is pretty baked in to the handling of hashes.

    One way to achieve what you want is to provide your own subclass of Psych’s ToRuby class and override this method, so that it just treats mapping keys of << as literals. This involves overriding a private method in Psych, so you need to be a little careful:

    require 'psych'
    
    class ToRubyNoMerge < Psych::Visitors::ToRuby
      def revive_hash hash, o
        @st[o.anchor] = hash if o.anchor
    
        o.children.each_slice(2) { |k,v|
          key = accept(k)
          hash[key] = accept(v)
        }
        hash
      end
    end
    

    You would then use it like this:

    tree = Psych.parse your_data
    data = ToRubyNoMerge.new.accept tree
    

    With the Yaml from your example, data would then look something like

    {"defaults"=>{"foo"=>"bar", "zip"=>"button"},
     "node"=>{"<<"=>{"foo"=>"bar", "zip"=>"button"}, "foo"=>"other"}}
    

    Note the << as a literal key. Also the hash under the data["defaults"] key is the same hash as the one under the data["node"]["<<"] key, i.e. they have the same object_id. You can now manipulate the data as you want, and when you write it out as Yaml the anchors and aliases will still be in place, although the anchor names will have changed:

    data['node']['foo'] = "yet another"
    puts Yaml.dump data
    

    produces (Psych uses the object_id of the hash to ensure unique anchor names (the current version of Psych now uses sequential numbers rather than object_id)):

    ---
    defaults: &2151922820
      foo: bar
      zip: button
    node:
      <<: *2151922820
      foo: yet another
    

    If you want to have control over the anchor names, you can provide your own Psych::Visitors::Emitter. Here’s a simple example based on your example and assuming there’s only the one anchor:

    class MyEmitter < Psych::Visitors::Emitter
      def visit_Psych_Nodes_Mapping o
        o.anchor = 'defaults' if o.anchor
        super
      end
    
      def visit_Psych_Nodes_Alias o
        o.anchor = 'defaults' if o.anchor
        super
      end
    end
    

    When used with the modified data hash from above:

    #create an AST based on the Ruby data structure
    builder = Psych::Visitors::YAMLTree.new
    builder << data
    ast = builder.tree
    
    # write out the tree using the custom emitter
    MyEmitter.new($stdout).accept ast
    

    the output is:

    ---
    defaults: &defaults
      foo: bar
      zip: button
    node:
      <<: *defaults
      foo: yet another
    

    (Update: another question asked how to do this with more than one anchor, where I came up with a possibly better way to keep anchor names when serializing.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to open a page inside another page without the horizontal scroll bar
I have a zip password file and know this password. I need open this
i need open the project exe file from inside the visual studio 2008
I need to open a file as std::fstream (or actually any other std::ostream) when
i need to open the following url $file = http://en.wikipedia.org/w/api.php?action=parse&page=Kundapura&prop=text&format=xml; $fp = fopen($file, r);
I need to open a log file for writing. Trouble is, many things may
I need to open pdf file into browser without Adobe Reader. Is there any
i need to open an Apple unsupported file in my ipad app using Open
I need to open a *.bat file in a popup window. The way i
I need to open an XML file exclusively, make a modification, and save it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.