Skip to content

Instantly share code, notes, and snippets.

@ddlsmurf
Last active May 22, 2021 11:27
Show Gist options
  • Save ddlsmurf/1590434 to your computer and use it in GitHub Desktop.
Save ddlsmurf/1590434 to your computer and use it in GitHub Desktop.
YAML succinct tutorial #tutorial #yaml
# Example with basic types
---
hash:
  string: some text
  number: 12345
  array_of_bool: [on, off, true, false, yes, no]
  ruby symbol: :symbol
  array:
    - item one
    - item two
-->
{
    "hash" => {
               "number" => 12345,
        "array_of_bool" => [
            [0] true,
            [1] false,
            [2] true,
            [3] false,
            [4] true,
            [5] false
        ],
                "array" => [
            [0] "item one",
            [1] "item two"
        ],
               "string" => "some text",
          "ruby symbol" => :symbol
    }
}
---
# Example with array of hash :
---
- key:
  sub_key: value
-->
[
    [0] {
        "sub_key" => "value",
            "key" => nil
    }
]
---
# Example with array of hash of hash (nb: double indent) :
---
- key:
    sub_key: value
-->
[
    [0] {
        "key" => {
            "sub_key" => "value"
        }
    }
]
---
# Example with value and hash anchors and aliases:
---
key_one: &anchor_val 123
key_two: &anchor
  sub_key: *anchor_val
key_three: *anchor
-->
{
    "key_three" => {
        "sub_key" => 123
    },
      "key_two" => {
        "sub_key" => 123
    },
      "key_one" => 123
}
---
# Example with hash anchor and flow merges
---
key_one: &anchor
  sub_key_one: value one
  sub_key_two: value two
key_two:
  sub_key_three: value three
  <<: *anchor
-->
{
    "key_two" => {
        "sub_key_three" => "value three",
          "sub_key_two" => "value two",
          "sub_key_one" => "value one"
    },
    "key_one" => {
        "sub_key_two" => "value two",
        "sub_key_one" => "value one"
    }
}
---
# Example with hash anchor and flow merges and collision
---
key_one: &anchor
  sub_key_one: value one
  sub_key_two: value two
key_two:
  sub_key_two: new_value_before_anchor
  <<: *anchor
  sub_key_two: new_value_after_anchor
  sub_key_three: value three
-->
{
    "key_two" => {
        "sub_key_three" => "value three",
          "sub_key_two" => "new_value_after_anchor",
          "sub_key_one" => "value one"
    },
    "key_one" => {
        "sub_key_two" => "value two",
        "sub_key_one" => "value one"
    }
}
---
#!/usr/bin/env ruby -rubygems
printer = :awesome_print
begin
require printer.to_s
rescue LoadError => e
unless printer == :pp
printer = :pp
retry
end
raise
end
require "yaml"
def yaml_examples
this_funcs_indent = /^ /
yield <<-YAML.gsub(this_funcs_indent, "")
# Example with basic types
---
hash:
string: some text
number: 12345
array_of_bool: [on, off, true, false, yes, no]
ruby symbol: :symbol
array:
- item one
- item two
YAML
yield <<-YAML.gsub(this_funcs_indent, "")
# Example with array of hash :
---
- key:
sub_key: value
YAML
yield <<-YAML.gsub(this_funcs_indent, "")
# Example with array of hash of hash (nb: double indent) :
---
- key:
sub_key: value
YAML
yield <<-YAML.gsub(this_funcs_indent, "")
# Example with value and hash anchors and aliases:
---
key_one: &anchor_val 123
key_two: &anchor
sub_key: *anchor_val
key_three: *anchor
YAML
yield <<-YAML.gsub(this_funcs_indent, "")
# Example with hash anchor and flow merges
---
key_one: &anchor
sub_key_one: value one
sub_key_two: value two
key_two:
sub_key_three: value three
<<: *anchor
YAML
yield <<-YAML.gsub(this_funcs_indent, "")
# Example with hash anchor and flow merges and collision
---
key_one: &anchor
sub_key_one: value one
sub_key_two: value two
key_two:
sub_key_two: new_value_before_anchor
<<: *anchor
sub_key_two: new_value_after_anchor
sub_key_three: value three
YAML
end
yaml_examples do |yaml_str|
puts yaml_str
puts "-->"
__send__(printer, YAML.load(yaml_str))
puts "---\n\n\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment