ANN: scanf for Ruby

This is the product of the Austin Ruby Codefest 2002,
a weekend event which concluded on Wednesday.

Contributing were David Alan Black, Hal Fulton,
Nolan Darilek, and Jason Johnston.

This is a pure Ruby implementation of (most of)
the C scanf(3) function.

Look in the RAA or at this URL:
http://rubyhacker.com/code/scanf/

There are scanf methods added to String, IO, and
Kernel (corresponding to C’s sscanf(), fscanf(),
and plain vanilla scanf(), respectively).

There are over 130 testcases, for those who
want to know that.

Rationale for creating it:

  1. You can use regular expressions to retrieve
    substrings, but they’re not converted automatically
    to Fixnum or Float. This takes care of that.
  2. Given printf, sprintf, and String#%, scanf
    makes sense for more completeness.
  3. Another bone tossed to C programmers. Those
    brain cells you used learning format strings won’t
    go to waste.

Thanks,
Hal Fulton

Hal E. Fulton wrote:

This is the product of the Austin Ruby Codefest 2002,
a weekend event which concluded on Wednesday.

Contributing were David Alan Black, Hal Fulton,
Nolan Darilek, and Jason Johnston.

This is a pure Ruby implementation of (most of)
the C scanf(3) function.

Look in the RAA or at this URL:
http://rubyhacker.com/code/scanf/

There are scanf methods added to String, IO, and
Kernel (corresponding to C’s sscanf(), fscanf(),
and plain vanilla scanf(), respectively).

There are over 130 testcases, for those who
want to know that.

Rationale for creating it:

  1. You can use regular expressions to retrieve
    substrings, but they’re not converted automatically
    to Fixnum or Float. This takes care of that.
  2. Given printf, sprintf, and String#%, scanf
    makes sense for more completeness.
  3. Another bone tossed to C programmers. Those
    brain cells you used learning format strings won’t
    go to waste.

Thanks,
Hal Fulton

Very nice…thanks guys!

Hi,

···

In message “ANN: scanf for Ruby” on 02/08/22, “Hal E. Fulton” hal9000@hypermetrics.com writes:

This is the product of the Austin Ruby Codefest 2002,
a weekend event which concluded on Wednesday.

Contributing were David Alan Black, Hal Fulton,
Nolan Darilek, and Jason Johnston.

This is a pure Ruby implementation of (most of)
the C scanf(3) function.

Look in the RAA or at this URL:
http://rubyhacker.com/code/scanf/

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.

						matz.

Thanks, Matz…

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.

Hi,

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:

(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.

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?

I meant like this:

Hi –

···

On Thu, 29 Aug 2002, Yukihiro Matsumoto wrote:

Hi,

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 :slight_smile:

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Thu, 29 Aug 2002, Hal E. Fulton wrote:

----- 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.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav