I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.
I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.
Depending on your platform, you might find that using popen with tail -1
will be the fastest and easiest way. Failing that, consider this:
The idea is that you put the file object in binmode and pick a chunk of bytes towards the end of the file using for example
file.seek(-chunk_size_in_bytes, IO::SEEK_END)
and see whether the last (or last-but-one) line separator is there. If it is not, step a bit backwards and try again.
-- fxn
···
On Nov 19, 2007, at 3:38 PM, Shuaib Zahda wrote:
I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.
On Nov 19, 9:38 am, Shuaib Zahda <shuaib.za...@gmail.com> wrote:
Hi all
I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.
I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.
On Mon, Nov 19, 2007 at 11:38:08PM +0900, Shuaib Zahda wrote:
> Hi all
>
> I was trying to find a method to read only the last line of a file. I
> checked the the documentation and there is no such method.
> Beside reading line after line and checking the the eof method. Is there
> any faster or shorter way to do that.
Depending on your platform, you might find that using popen with tail -1
will be the fastest and easiest way. Failing that, consider this:
> > Hi all
> >
> > I was trying to find a method to read only the last line of a file. I
> > checked the the documentation and there is no such method.
> > Beside reading line after line and checking the the eof method. Is there
> > any faster or shorter way to do that.
>
> Depending on your platform, you might find that using popen with tail -1
> will be the fastest and easiest way. Failing that, consider this:
>
> last = File.open(myfile) { |f| f.extend(Enumerable).inject { |_,ln| ln } }
You do not need the ".extend(Enumerable)" bit as an IO does already
include Enumerable:
$ ruby -e 'p Enumerable === $stdin'
true
Ah, sorry, you are correct. For some reason I was thinking of MySQL result
sets.
Cheers
robert
--Greg
···
On Tue, Nov 20, 2007 at 12:41:46AM +0900, Robert Klemme wrote:
2007/11/19, Gregory Seidman <gsslist+ruby@anthropohedron.net>:
> On Mon, Nov 19, 2007 at 11:38:08PM +0900, Shuaib Zahda wrote:
I was trying to figure out how to use this method but I failed. Can u
please give me an example
require "elif"
last_line = Elif.open(path) { |f| f.gets }
James Edward Gray II
Thanks it works. But when i used require "elif". My ruby did not
recognize its path. So i copied it into my local directory and ran it
fine. Is there any path issue that i have to tackle when i download new
package from gem. I am using ubuntu 7.10
# Thanks it works. But when i used require "elif". My ruby did not
# recognize its path. So i copied it into my local directory and ran it
# fine. Is there any path issue that i have to tackle when i
# download new package from gem. I am using ubuntu 7.10
try,
require 'rubygems'
gems require rubygems to find themselves
require is change thereupon..
:~$ irb
~> alias old_require require
=> nil
~> require 'units'
LoadError: no such file to load -- units
~> old_require 'units'
LoadError: no such file to load -- units
~> require 'rubygems'
=> true
~> old_require 'units'
LoadError: no such file to load -- units
~> require 'units'
=> true
short story: only rubygems knows where the gems are
From: Shuaib Zahda [mailto:shuaib.zahda@gmail.com]
# Thanks it works. But when i used require "elif". My ruby did not
# recognize its path. So i copied it into my local directory and ran it
# fine. Is there any path issue that i have to tackle when i
# download new package from gem. I am using ubuntu 7.10
try,
require 'rubygems'
gems require rubygems to find themselves
require is change thereupon..
Thanks a lot. It works well. So, I can conclude that I have to call
rubygems then I call the elif.