Read "current line" from a file?

All,

Given an IO object, you can get the “next” line with #gets, right?

Given an IO object, you can get the “current” line with #X, right? Please?

I know it’s not a common operation, but I would like tobe able to do it.

···


Gavin Sinclair Software Engineer
Sydney, Australia Soyabean Software Pty Ltd

Hello

Yo can go back one line and gets

class File
def current
self.lineno=self.lineno-1
self.gets
end
end

hope it helps and it’s what you are looking for

bye

···

On Thu 24 Oct 2002 11:57, Gavin Sinclair wrote:

All,

Given an IO object, you can get the “next” line with #gets, right?

Given an IO object, you can get the “current” line with #X, right? Please?

I know it’s not a common operation, but I would like tobe able to do it.


Gavin Sinclair Software Engineer
Sydney, Australia Soyabean Software Pty Ltd


Javier Fontan Muiños
jfontan@cesga.es
CESGA, Supercomputing Center of Galicia

Hi –

All,

Given an IO object, you can get the “next” line with #gets, right?

Given an IO object, you can get the “current” line with #X, right? Please?

I know it’s not a common operation, but I would like tobe able to do it.

Do you mean a kind of one-line lookahead? I had occasion to do that
somewhere recently, and did:

class File
def ungets(str)
seek(pos - str.size,0)
end

# One-line lookahead.
def next_line
  line = gets
  ungets(line) if line
  line
end

end

David

···

On Thu, 24 Oct 2002, Gavin Sinclair wrote:


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

Hi,

Given an IO object, you can get the “next” line with #gets, right?

Given an IO object, you can get the “current” line with #X, right? Please?

What about this?

class IO
module Backing
def gets(rs = $/)
@backing = super
end
def last
@backing
end
end
end

STDIN.extend(IO::Backing)
p STDIN.gets
p STDIN.last

···

At Thu, 24 Oct 2002 18:57:49 +0900, Gavin Sinclair wrote:


Nobu Nakada

Hi –

···

On Thu, 24 Oct 2002, Javier Fontan wrote:

Hello

Yo can go back one line and gets

class File
def current
self.lineno=self.lineno-1
self.gets
end
end

I can’t get that to work. Do you have a working example?

David


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

Hi –

All,

Given an IO object, you can get the “next” line with #gets, right?

Given an IO object, you can get the “current” line with #X, right? Please?

I know it’s not a common operation, but I would like tobe able to do it.

Do you mean a kind of one-line lookahead? I had occasion to do that
somewhere recently, and did:

class File
def ungets(str)
seek(pos - str.size,0)
end

# One-line lookahead.
def next_line
  line = gets
  ungets(line) if line
  line
end

end

David

Hmmm… I might give that a go. This is the sort of thing I’m trying to do.
I’ve got the following code:

  file.reverse_each do |line|
    if line =~ /Published/
      timestamp = collect_timestamp(line)
      line = file.gets && file.gets
      unless line.nil?
        backend_id = collect_backend_id(line)
        ...
      end
    end
  end

And I’d like to refactor it to:

  file.reverse_each do |line|
    if line =~ /Published/
      timestamp, backend_id = collect_data(file)
    end
  end

  def collect_data(file)
    timestamp = collect_timestamp(file.current_line)
    line = file.gets && file.gets
    unless line.nil?
      backend_id = collect_backend_id(line)
      ...
    end
  end

File#reverse_each is a nifty method I wrote to process a file in reverse line
order. I was (obviously) hoping that IO/File would have a buffer of the
current line that it can share with me! If I have to hack it, then I don’t
really achieve anything pretty.

Thanks,
Gavin

···

From: dblack@candle.superlink.net

On Thu, 24 Oct 2002, Gavin Sinclair wrote:

Hi,

Given an IO object, you can get the “next” line with #gets, right?

Given an IO object, you can get the “current” line with #X, right? Please?

What about this?

class IO
module Backing
def gets(rs = $/)
@backing = super
end
def last
@backing
end
end
end

STDIN.extend(IO::Backing)
p STDIN.gets
p STDIN.last

That’s quite nice. I pronounce this the best solution so far :slight_smile:

Nobu Nakada

Gavin

···

From: nobu.nokada@softhome.net

At Thu, 24 Oct 2002 18:57:49 +0900, > Gavin Sinclair wrote:

Wouldn’t you need:
@backing = super(rs)
to be correct?

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.10.24 at 13.07.23

···

On Thu, 24 Oct 2002 21:35:55 +0900, nobu.nokada@softhome.net wrote:

class IO
module Backing
def gets(rs = $/)
@backing = super
end
def last
@backing
end
end
end

STDIN.extend(IO::Backing)
p STDIN.gets
p STDIN.last

Wouldn't you need:
    @backing = super(rs)
to be correct?

You have found one reason why `super' is a keyword and not a method :slight_smile:

`super' without () propagate the arguments given to the methods.

Guy Decoux