Merging (add) two yaml objects into one

I would like to merge two yaml objects into one. Something like this:

o1 = YAML.load_file(‘file1.yml’)
o2 = YAML.load_file(‘file2.yml’)

o3 = o1 + o2

or

o1 << o2

I can’t find this in documentation or is it not implemented.

by
TheR

Damjan R. wrote in post #1134248:

I would like to merge two yaml objects into one.

There’s no such thing as “yaml objects” the way you’re saying it. What
o1 and o2 are depend on the files. For example, this is a hash:


foo: 1
bar: 2

… and this is an array:


  • a
  • b
  • c

… etc. So, if file1 and file2 both look like the first one, you can
use regular hash methods, like #merge.

o1 = YAML.load_file(‘file1.yml’)
o2 = YAML.load_file(‘file2.yml’)
o3 = o1.merge o2

Note: the YAML files could contain any Ruby objects, including
arbitrary classes; but once they’re loaded into Ruby they’re just
regular Ruby objects.

I would have guessed it before if I would write:

p o1.class # => Hash

Thank you

TheR

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs