Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created May 8, 2015 03:49
Show Gist options
  • Save tenderlove/8c3988b8f797dfee0a3a to your computer and use it in GitHub Desktop.
Save tenderlove/8c3988b8f797dfee0a3a to your computer and use it in GitHub Desktop.
require 'psych'
def tree o, options = {}
visitor = Psych::Visitors::YAMLTree.create options
visitor << o
visitor.tree
end
# create a YAML AST from a Ruby object
my_object = [ "(( foo ))" ]
t = tree my_object
# find scalar nodes and modify them
t.grep(Psych::Nodes::Scalar).each do |node|
node.plain = true
node.quoted = false
node.style = Psych::Nodes::Scalar::PLAIN
end
puts t.yaml
# To figure out what the values *should* be, I usually parse the YAML to an
# AST and look at the values there:
p Psych.parse "(( foo ))"
@drnic
Copy link

drnic commented May 8, 2015

Thanks @tenderlove!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment