There are scanf methods added to String, IO, and
Kernel (corresponding to C’s sscanf(), fscanf(),
and plain vanilla scanf(), respectively).
This sounds great. Can I put this into the standard 1.7 distribution?
Only comment of mine for improvement is you don’t have to include
Scanf module to Kernel, IO, String. You just need to full qualified
class name, e.g Scanf::FormatSpecifier.
I’m sure all of us involved would love to see it
in the standard distribution.
As for your comment about including the module:
I guess what you mean is that we should do a
‘def’ in each of those three places, and use the
qualified name inside that definition, correct?
I think David has changed the source since the
last version I have. I will pass this on to him.
Hal Fulton
···
----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 28, 2002 2:39 AM
Subject: Re: ANN: scanf for Ruby
In message “ANN: scanf for Ruby” > on 02/08/22, “Hal E. Fulton” hal9000@hypermetrics.com writes:
There are scanf methods added to String, IO, and
Kernel (corresponding to C’s sscanf(), fscanf(),
and plain vanilla scanf(), respectively).
This sounds great. Can I put this into the standard 1.7 distribution?
Only comment of mine for improvement is you don’t have to include
Scanf module to Kernel, IO, String. You just need to full qualified
class name, e.g Scanf::FormatSpecifier.
As for your comment about including the module:
I guess what you mean is that we should do a
‘def’ in each of those three places, and use the
qualified name inside that definition, correct?
I think David has changed the source since the
last version I have. I will pass this on to him.
I meant like this:
class IO
def scanf(fstr)
start = pos
fs = Scanf::FormatString.new(fstr)
buffer = “”
matched = 0
m =
loop do
break if eof
buffer << gets
m.concat fs.match(buffer).compact
s = buffer.size
buffer = buffer[fs.pos..-1] || ""
matched += s - buffer.size if buffer
break if fs.done? || fs.fatal?
fs.last.times do fs.shift end
end
seek(matched + start, IO::SEEK_SET) rescue Errno::ESPIPE
m
end
end
class String
def scanf(fs)
Scanf::FormatString.new(fs).match(self).compact
end
end
matz.
···
In message “Re: ANN: scanf for Ruby” on 02/08/28, “Hal E. Fulton” hal9000@hypermetrics.com writes:
FYI, David has now added a “block” form of scanf…
I think it is interesting, but I have not seen the
need for it yet. I believe it effectively does a
“map” on the list of data items before returning it.
Easy to implement, and potentially useful. May as well
do it “because we can.”
Hal
···
----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 28, 2002 11:22 AM
Subject: Re: ANN: scanf for Ruby
As for your comment about including the module:
I guess what you mean is that we should do a
‘def’ in each of those three places, and use the
qualified name inside that definition, correct?
In message “Re: ANN: scanf for Ruby” > on 02/08/28, “Hal E. Fulton” hal9000@hypermetrics.com writes:
As for your comment about including the module:
I guess what you mean is that we should do a
‘def’ in each of those three places, and use the
qualified name inside that definition, correct?
I think David has changed the source since the
last version I have. I will pass this on to him.
I meant like this:
class IO
def scanf(fstr)
start = pos
fs = Scanf::FormatString.new(fstr)
(etc.)
The new version was supposed to do that, though I now notice a couple
of include’s I forgot to delete… and one unqualified constant… but
anyway, now it does it
----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, August 28, 2002 11:22 AM
Subject: Re: ANN: scanf for Ruby
As for your comment about including the module:
I guess what you mean is that we should do a
‘def’ in each of those three places, and use the
qualified name inside that definition, correct?
I meant like this:
(snip)
Yes, I understand.
FYI, David has now added a “block” form of scanf…
I think it is interesting, but I have not seen the
need for it yet. I believe it effectively does a
“map” on the list of data items before returning it.
Also it (block version) keeps scanning as long as it’s getting
results – so potentially it saves you from having to write your
own loop around scanf and doing your own eof tests and such.