Didn't see this discussed in the archives...if I just missed it, I would appreciate a pointer to the discussion.
Is it possible (or would it be difficult to add a way) to discover the line number on which a value in a YAML document was defined? I'm thinking of Copland, which uses YAML configuration files; it would be nice to say to a user: "oops, you specified a service that doesn't exist *at this line number in this configuration file*". It would make it a lot easier to debug Copland-based apps, anyway.
···
--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis
"I use octal until I get to 8, and then I switch to decimal."
Jamis Buck wrote:
Is it possible (or would it be difficult to add a way) to discover the line number on which a value in a YAML document was defined?
Not in the Ruby extension. This data is available in Syck, so it's possible. Hmm-m-m. Do you have any suggestions for how you want to retrieve this? Psuedo-code?
_why
why the lucky stiff wrote:
Jamis Buck wrote:
Is it possible (or would it be difficult to add a way) to discover the line number on which a value in a YAML document was defined?
Not in the Ruby extension. This data is available in Syck, so it's possible. Hmm-m-m. Do you have any suggestions for how you want to retrieve this? Psuedo-code?
_why
.
Perhaps something like this:
conf = YAML.load( File.read( "package.yml" ) )
p conf['id'] #-> "my.package.id"
p conf['id'].line_number #-> 2
The above approach would give you the number of the first line on which the requested value was defined. To find the line number on which the key was defined, maybe something like this:
conf.keys.each do { |key| p key.line_number }
Because dynamically adding a line_number attribute to every object read by the YAML parser would be prohibitively expensive for some applications, you may even want to make it optional:
YAML.line_numbers = true
conf = YAML.load(...)
...
Anyway, something like that would be sufficient for my needs. 
···
--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis
"I use octal until I get to 8, and then I switch to decimal."
but how would that work with something like this:
yaml = <<-yaml
···
On Sat, 21 Aug 2004, Jamis Buck wrote:
Perhaps something like this:
conf = YAML.load( File.read( "package.yml" ) )
p conf['id'] #-> "my.package.id"
p conf['id'].line_number #-> 2
The above approach would give you the number of the first line on which
the requested value was defined. To find the line number on which the
key was defined, maybe something like this:
conf.keys.each do { |key| p key.line_number }
Because dynamically adding a line_number attribute to every object read
by the YAML parser would be prohibitively expensive for some
applications, you may even want to make it optional:
YAML.line_numbers = true
conf = YAML.load(...)
...
Anyway, something like that would be sufficient for my needs. 
---
- array element 0
- array element 1
- array element 2
yaml
array = YAML.load yaml
p array.line_number # => 1
so each object returns would have the method 'line_number' added? guess that
could work. perhaps something like
obj.yaml_lineno
though, since 'line_number' is a bit generic.
cheers.
-a
--
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen
===============================================================================
Jamis Buck wrote:
Because dynamically adding a line_number attribute to every object read by the YAML parser would be prohibitively expensive for some applications, you may even want to make it optional:
YAML.line_numbers = true
conf = YAML.load(...)
...
Well, yeah. You'll never need the line number for every object, so here's what I'm thinkin:
conf = YAML.load_file( "package.yml" )
YAML.line_number_of( conf['id'] )
Which basically amounts to:
parser = YAML::Parser.new
conf = parser.load( "package.yml" )
line = parser.line_number_of( conf['id'] )
The lookup table gets stored in the extension.
_why
why the lucky stiff wrote:
Well, yeah. You'll never need the line number for every object, so here's what I'm thinkin:
conf = YAML.load_file( "package.yml" )
YAML.line_number_of( conf['id'] )
Which basically amounts to:
parser = YAML::Parser.new
conf = parser.load( "package.yml" )
line = parser.line_number_of( conf['id'] )
The lookup table gets stored in the extension.
That would work fine for my purposes as well. Whichever ends up being easier to code, I guess. I'm not picky. 
- Jamis
···
--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis
"I use octal until I get to 8, and then I switch to decimal."