Skip to content

Instantly share code, notes, and snippets.

@abinoam
Created August 11, 2013 03:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abinoam/6203235 to your computer and use it in GitHub Desktop.
Save abinoam/6203235 to your computer and use it in GitHub Desktop.
require 'psych'
class ScalarHandler < Psych::Handler
def parser=(parser)
@parser=parser
end
def mark
@parser.mark
end
def scalar value, anchor, tag, plain, quoted, style
p [value, anchor, tag, plain, quoted, style]
p mark
end
end
scalar_handler = ScalarHandler.new
parser = Psych::Parser.new(scalar_handler)
scalar_handler.parser = parser
yaml_document = <<END
foo:
bar:
baz: 3
blubb:
baz: 4
END
parser.parse(yaml_document)
=begin
["foo", nil, nil, true, false, 1]
#<struct Psych::Parser::Mark index=4, line=0, column=4>
["bar", nil, nil, true, false, 1]
#<struct Psych::Parser::Mark index=11, line=1, column=6>
["baz", nil, nil, true, false, 1]
#<struct Psych::Parser::Mark index=20, line=2, column=8>
["3", nil, nil, true, false, 1]
#<struct Psych::Parser::Mark index=25, line=3, column=2>
["blubb", nil, nil, true, false, 1]
#<struct Psych::Parser::Mark index=31, line=3, column=8>
["baz", nil, nil, true, false, 1]
#<struct Psych::Parser::Mark index=40, line=4, column=8>
["4", nil, nil, true, false, 1]
#<struct Psych::Parser::Mark index=43, line=5, column=0>
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment