# YAML: Find line number of key?

**URL:** <https://www.ruby-forum.com/t/yaml-find-line-number-of-key/246003>\
**Category:** Ruby\
**Created:** [August 9, 2013, 5:57pm UTC](https://www.ruby-forum.com/t/yaml-find-line-number-of-key/246003 "2013-08-09T17:57:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [August 9, 2013, 5:57pm UTC](https://www.ruby-forum.com/t/yaml-find-line-number-of-key/246003/1 "2013-08-09T17:57:43Z")

</div>

Hi there,

I’ve been digging around in Psych’s lowlevel interface the last few  
hours, but I couldn’t find what I was looking for. Given this YAML file:

* * *

## foo: bar: baz: 3 blubb: baz: 4

How can I find the line number the key(chain) foo-bar-baz is in (that  
is, 3 in this case)? I’ve found Psych supports a nice visitor pattern  
for parsing, but it seems to not provide the line numbers of the  
elements it encounters. So, is there any way (monkey-patching if  
necessary) to get the line numbers out of Psych?

Background: I want to programmatically insert comments into an existing  
YAML file and as I’ve already found out, inserting comments with Psych  
is impossible. That is, I want to say “insert this comment before the  
key foo-bar-baz”.

Thanks in advance.  
Marvin

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [August 11, 2013, 5:21am UTC](https://www.ruby-forum.com/t/yaml-find-line-number-of-key/246003/2 "2013-08-11T05:21:15Z")

</div>

Hi Marvin,

I don’t have experience with psych but taking a look at the  
documentation I’ve managed to do the code bellow.  
(Perhaps there’s a clever way to do it).  
I tried to “craft” a Handler that have access to the Parser instance  
so that it could call #mark on it.

Handler

> **[Class: Psych::Handler (Ruby 1.9.3)](https://ruby-doc.org/stdlib-1.9.3/libdoc/psych/rdoc/Psych/Handler.html)**
>
> Class : Psych::Handler - Ruby 1.9.3

#mark

> **[Class: Psych::Parser (Ruby 1.9.3)](https://ruby-doc.org/stdlib-1.9.3/libdoc/psych/rdoc/Psych/Parser.html)**
>
> Class : Psych::Parser - Ruby 1.9.3

> <https://gist.github.com/abinoam/6203235>

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)

[“foo”, nil, nil, true, false, 1]  
#  
[“bar”, nil, nil, true, false, 1]  
#  
[“baz”, nil, nil, true, false, 1]  
#  
[“3”, nil, nil, true, false, 1]  
#  
[“blubb”, nil, nil, true, false, 1]  
#  
[“baz”, nil, nil, true, false, 1]  
#  
[“4”, nil, nil, true, false, 1]  
#

Does this help you start things up?  
(Sorry if this is not the point (if I didn’t get it right)).

Abinoam Jr.

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [August 11, 2013, 9:28am UTC](https://www.ruby-forum.com/t/yaml-find-line-number-of-key/246003/3 "2013-08-11T09:28:22Z")

</div>

Am Sun, 11 Aug 2013 00:20:24 -0300  
schrieb “Abinoam Jr.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi Marvin,
> 
> I don’t have experience with psych but taking a look at the  
> documentation I’ve managed to do the code bellow.  
> (Perhaps there’s a clever way to do it).  
> I tried to “craft” a Handler that have access to the Parser instance  
> so that it could call #mark on it.

This looks very good! Thank you for your help, I
