Created
August 11, 2013 03:17
-
-
Save abinoam/6203235 to your computer and use it in GitHub Desktop.
Trying to answer to https://www.ruby-forum.com/topic/4416285
This file contains 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' | |
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