public
Created

  • Download Gist
psych_handler_with_mark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.