How to Specify Formatting Options for To_Yaml in Ruby

Is it possible to specify formatting options for to_yaml in ruby?

This ugly hack seems to do the trick...

class Array
def to_yaml_style
:inline
end
end

Browsing through ruby's source, I can't find any options I could pass to achieve the same. Default options are described in the lib/yaml/constants.rb.

Documentation for Psych to_yaml options?

Deep in the guts of ruby-1.9.3-p125/ext/psych/emitter.c I found three options:

  • indentation - The level must be less than 10 and greater than 1.
  • line_width - Set the preferred line width.
  • canonical - Set the output style to canonical, or not (true/false).

And they work!

Writing to YAML file with File.open wraps longer lines

Turns out this was actually answered previously in "why does psych yaml interpreter add line breaks around 80 characters?". I was searching for the wrong thing.

Doing something like

yaml.to_yaml(:options => {:line_width => -1})

keeps the lines from wrapping.

ruby to_yaml 2.0.0 adds !ruby/object:Hash but YAML.load won't read it

Problem found: I modified hash.to_yaml so that it sorts the keys. Since my code is based on hash.to_yaml 1.8.7 I need to revise it based on hash.to_yaml 2.0.0.

ActiveRecords not serialized to yaml correctly

The problem turned out to be interactions between a few different environment issues that I didn't mention in the original question.

So the problem turned out to be a configuration issue with passenger. If you have a file in your project at config/setup_load_paths.rb then your Gemfile isn't loaded. I'd added

require "yaml"
YAML::ENGINE.yamler = "syck"

at the top of my Gemfile to ensure that the engine was selected before rails loaded and registered it's active record converter with yaml. Passenger wasn't running the Gemfile so the engine was never getting set and ruby was defaulting to pysch instead of syck. The current version of delayed_job forces the use of syck but because psych was being loaded the syck engine never had the active record converter registered with it.

How to convert symbols to strings (i.e. strip leading :) ruby to_yaml

You need to have keys as strings to skip : generation

require 'active_support/core_ext/hash/keys'
require 'yaml'

DEFAULT_BONUS_CONFIG.deep_stringify_keys.to_yaml.gsub("---\n", '')

=> "sign_up:\n currency: ngn\n type: flat\n amount: 1000\nvisit:\n currency: ngn\n type: flat\n amount: 5\n"

How to use to_yaml method in Rails, and remove '---'

Brute force, but it'll do the trick:

h.deep_stringify_keys.to_yaml[3..-1]

Ruby Appending comment block to YAML file

That the comments are lost with a dump is unfortunatly normal.
You have two options:

  1. convert your versions hash to yaml { :a => 'b'}.to_yaml, add the comments and with File.write do the dump yourself, you could overwrite the normal
    .dump method in YAML this way
  2. assign the comments to some dummy value at the end of your yaml file
    so that they are read into versions and saved also.


Related Topics



Leave a reply



Submit