J
Joel VanderWerf
I've got a file which is a '---' separated list of YAML docs. I can read
them all with
YAML.load_documents(file) {|doc| ...}
but what I'd really like to do is read one from the stream and come back
to the stream later and read it. I tried doing that with #load, but I
only get one of the documents and then the file pos goes to the end of
the file and the second document cannot be read:
$ cat >docs.yaml
---
a: 1
b: 2
---
a: 3
b: 4
$ irb -r yaml
irb(main):001:0> f = File.open('docs.yaml')
=> #<File:docs.yaml>
irb(main):002:0> YAML.load(f)
=> {"a"=>1, "b"=>2}
irb(main):003:0> YAML.load(f)
=> false
irb(main):004:0> f.rewind
=> 0
irb(main):008:0> YAML.load(f)
=> {"a"=>1, "b"=>2}
irb(main):009:0> f.pos
=> 28
irb(main):010:0> f.rewind
=> 0
irb(main):011:0> YAML.load_documents(f) {|d| p d}
{"a"=>1, "b"=>2}
{"a"=>3, "b"=>4}
I guess I could fire up a generator to externalize the iterator, but why
can't YAML#load just read one doc at a time and not advance the file
pointer farther than the end of the doc?
them all with
YAML.load_documents(file) {|doc| ...}
but what I'd really like to do is read one from the stream and come back
to the stream later and read it. I tried doing that with #load, but I
only get one of the documents and then the file pos goes to the end of
the file and the second document cannot be read:
$ cat >docs.yaml
---
a: 1
b: 2
---
a: 3
b: 4
$ irb -r yaml
irb(main):001:0> f = File.open('docs.yaml')
=> #<File:docs.yaml>
irb(main):002:0> YAML.load(f)
=> {"a"=>1, "b"=>2}
irb(main):003:0> YAML.load(f)
=> false
irb(main):004:0> f.rewind
=> 0
irb(main):008:0> YAML.load(f)
=> {"a"=>1, "b"=>2}
irb(main):009:0> f.pos
=> 28
irb(main):010:0> f.rewind
=> 0
irb(main):011:0> YAML.load_documents(f) {|d| p d}
{"a"=>1, "b"=>2}
{"a"=>3, "b"=>4}
I guess I could fire up a generator to externalize the iterator, but why
can't YAML#load just read one doc at a time and not advance the file
pointer farther than the end of the doc?