From: Konrad Meyer Date: 2007-10-28T11:34:52+09:00 Subject: Re: NOT reading an entire file into memory --nextPart2448670.z5XDEfIDvv Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth 7stud --: > Devi Web Development wrote: > > I have heard that File.readline is in fact equivalent to > > File.read.split('\n').each, which would really ruin my purpose of not > > loading the whole file.=20 > > >=20 > I doubt that is true, but as is often the case with Ruby there is no=20 > easily locatable documentation that describes File I/O buffering. Just=20 > in case, here is another solution: >=20 > #create a data file containing: > #line 1 > #line 2 > #... > #line 10 >=20 > File.open("data.txt", "w") do |file| > 10.times do |i| > file.puts("line #{i+1}") > end > end >=20 >=20 > #read lines 4-7 and display them: > File.open("data.txt") do |file| > file.each_with_index do |line, i| > i =3D i + 1 #i starts at 0 >=20 > if i < 4 > next > elsif i < 8 > puts line > else > break > end >=20 > end > end IO#each_with_index and IO#readline are probably the same internally, so the= =20 real answer here is that NO, IO#readline is NOT the same as=20 =46ile.read.split('\n'), that's IO#readlines. =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart2448670.z5XDEfIDvv Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHI/VJCHB0oCiR2cwRAroPAJ9PkmMnb0GaAuQGsF/PPgmMC+PkQQCfc+v8 7wxmgvidh89S15qXTNe5Yq4= =rHhI -----END PGP SIGNATURE----- --nextPart2448670.z5XDEfIDvv--