Created
May 8, 2015 03:49
-
-
Save tenderlove/8c3988b8f797dfee0a3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ))" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @tenderlove!