Isolating YAML for an array in a hash

I think indentation is important in YAML.

I tested the following in irb :

 > puts({1=>[1,2,3]}.to_yaml) --- 1: - 1 - 2 - 3 => nil 

I was expecting something like this:

 > puts({1=>[1,2,3]}.to_yaml) --- 1: - 1 - 2 - 3 => nil 

Why is there no indentation for the array?

I found this at http://www.yaml.org/YAML_for_ruby.html#collections .

A dash in a sequence is considered indented, so you can add a sequence within the display without requiring spaces as indentation.

+7
source share
2 answers

Both methods are valid, as far as I can tell:

 require 'yaml' YAML.load(%q{--- 1: - 1 - 2 - 3 }) # => {1=>[1, 2, 3]} YAML.load(%q{--- 1: - 1 - 2 - 3 }) # => {1=>[1, 2, 3]} 

It’s not clear why you think there should be spaces before the hyphen. If you think this is a spec violation, explain how to do it.

Why is there no indentation for the array?

There is no need to fall back before a hyphen, and it is easier not to add it.

+3
source

So you can do:

 1: - 2: 3 4: 5 - 6: 7 8: 9 - 10 => {1 => [{2 => 3, 4 => 5}, {6 => 7, 8 => 9}, 10]} 

Basically, dashes highlight objects, and indentation means the "value" of a key-value pair.

This is the best I can do; I could not find any reasons for this or that aspect of the syntax.

+6
source

All Articles