The first program prints the contents of foo and exits. The second program waits for ^D from the terminal, and then tries to parse the YAML and go on with life.
I want ARGF to behave like it does in the first example, in the second example. Of course I could do YAML.load(ARGF.read) but that doesn't satisfy my curiousity.
IMO, YAML.load *should* do this with ARGF because the following works:
f = File.open("foo", "rb")
YAML.load(f)
f.close
-austin
···
On Thu, 5 Aug 2004 02:36:29 +0900, Hans Fugal <hans@fugal.net> wrote:
Consider these two short programs:
ARGF.read
and
require 'yaml'
YAML.load(ARGF)
Then call them like so:
$ ruby foo.rb foo
The first program prints the contents of foo and exits. The second
program waits for ^D from the terminal, and then tries to parse the YAML
and go on with life.
I want ARGF to behave like it does in the first example, in the second
example. Of course I could do YAML.load(ARGF.read) but that doesn't
satisfy my curiousity.
... waits for ^D from the terminal, and then tries to parse the YAML and go on with life.
Try:
require 'yaml'
until ARGF.closed?
YAML::load( ARGF.file )
ARGF.close
end
Like Robert said, ARGF isn't an IO object. If you run ARGF.to_io, you'll only get the first stream. ARGF is a set of streams. The question is: does each supplied stream separately contain a YAML document? Or do the streams represent chunks of a partitioned YAML document? Your example seems to indicate the second. But I would have found the first more natural.
"Austin Ziegler" <halostatue@gmail.com> schrieb im Newsbeitrag
news:9e7db91104080410463cea4c22@mail.gmail.com...
> Consider these two short programs:
> ARGF.read
> and
> require 'yaml'
> YAML.load(ARGF)
>
> Then call them like so:
>
> $ ruby foo.rb foo
>
> The first program prints the contents of foo and exits. The second
> program waits for ^D from the terminal, and then tries to parse the YAML
> and go on with life.
>
> I want ARGF to behave like it does in the first example, in the second
> example. Of course I could do YAML.load(ARGF.read) but that doesn't
> satisfy my curiousity.
IMO, YAML.load *should* do this with ARGF because the following works:
f = File.open("foo", "rb")
YAML.load(f)
f.close
Not so fast: ARGF is quite special. It's especially *no* an IO. It just
happens to implement some methods of IO. IMHO it's not reasonable to invoke
YAML.load(ARGF) because ARGF might draw its data from any number of files -
including stdin. IMHO it's not very practical to distribute YAML data
across multiple files. The case is differnt for grep like tools that can
reasonable operate on a multitude of files.
Just my 0.02EUR...
Kind regards
robert
···
On Thu, 5 Aug 2004 02:36:29 +0900, Hans Fugal <hans@fugal.net> wrote:
Like Robert said, ARGF isn't an IO object. If you run ARGF.to_io, you'll only get the first stream. ARGF is a set of streams. The question is: does each supplied stream separately contain a YAML document? Or do the streams represent chunks of a partitioned YAML document? Your example seems to indicate the second. But I would have found the first more natural.
I agree it doesn't always make sense, maybe not even often, but sometimes it does. There's nothing that says a yaml document has to reside in one file, and there's certainly good reason to take it in on stdin.
Robert Klemme wrote:
···
"Austin Ziegler" <halostatue@gmail.com> schrieb im Newsbeitrag
news:9e7db91104080410463cea4c22@mail.gmail.com...
On Thu, 5 Aug 2004 02:36:29 +0900, Hans Fugal <hans@fugal.net> wrote:
Consider these two short programs:
ARGF.read
and
require 'yaml'
YAML.load(ARGF)
Then call them like so:
$ ruby foo.rb foo
The first program prints the contents of foo and exits. The second
program waits for ^D from the terminal, and then tries to parse the YAML
and go on with life.
I want ARGF to behave like it does in the first example, in the second
example. Of course I could do YAML.load(ARGF.read) but that doesn't
satisfy my curiousity.
IMO, YAML.load *should* do this with ARGF because the following works:
f = File.open("foo", "rb")
YAML.load(f)
f.close
Not so fast: ARGF is quite special. It's especially *no* an IO. It just
happens to implement some methods of IO. IMHO it's not reasonable to invoke
YAML.load(ARGF) because ARGF might draw its data from any number of files -
including stdin. IMHO it's not very practical to distribute YAML data
across multiple files. The case is differnt for grep like tools that can
reasonable operate on a multitude of files.