File.each can start from bottom of file?

R

Roger

Instead of iterating from first to last line in file,
can the each method go from last to first?

If no, any ideas to achieve this?

Thanks,
Roger
 
D

Dave Burt

Roger said:
Instead of iterating from first to last line in file,
can the each method go from last to first?

If no, any ideas to achieve this?

If your file isn't huge, use IO.readlines(filename).reverse.each

Cheers,
Dave
 
R

Robert Klemme

Dave Burt said:
If your file isn't huge, use IO.readlines(filename).reverse.each

For efficiency reasons you can iterate in reverse without creating the
reversed array first:

IO.readlines(filename).reverse_each

And it is exactly the same amount of character typing. :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top