From: Jeremy Gregorio Date: 2001-11-04T04:47:05+09:00 Subject: [ruby-talk:24282] Some problems with file IO in the winruby... This is a multi-part message in MIME format. ------=_NextPart_000_000B_01C16401.B84A3360 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I installed winruby a while back. I've been using ruby from the = command line like so: ruby my_prog.rb and so far it's worked great, but = I came across something I can't figure out. I tried this code: if fileName !=3D nil then =20 IO.readlines(fileName).each{|line| =20 line.chomp! $lines << line =20 }#end IO.readlines...each =20 end #if fileName =20 while command =3D gets print command end #while Ideally it should read the contents of a file whose name I specify on = the command line and then prompt the user for input and print that = input. but the calls to gets are going to the file object opened by = IO.readlines. I can fix this with an explicit $stdin.get, but that's = kind of a hack. I can also throw in this code: while trash =3D gets end and it consumes the file object from IO.readlines and lets me get on = with reading from standard input. But again that's a hack. Is there some = way to force gets() back to reading from standard input. i.e. to = properly close out the file. I thought Using File.new and then explictly = closing the object it returns would do that, but this code: if fileName !=3D nil then inputFile =3D open fileName =20 begin inputFile.each {|line|=20 =20 line.chomp! $lines << lline }#end inputFile.each =20 ensure =20 inputFile.close end #end begin..ensure end #if has the same problem. Can someone tell me what's going on here. I'm = totally lost. Thanks again Jeremy Gregorio rsilvergun@home.net ------=_NextPart_000_000B_01C16401.B84A3360 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
    I installed winruby = a while=20 back. I've been using ruby from the command line like so: ruby = my_prog.rb and so=20 far it's worked great, but I came across something I can't figure out. I = tried=20 this code:
 
if fileName !=3D nil=20 then
 
 IO.readlines(fileName).each{|line|
 
&= nbsp; line.chomp!
  $lines=20 << line
  
 }#end=20 IO.readlines...each
  
end #if fileName
  =20
while command =3D gets
    print = command
 
end #while
 
Ideally it should read the contents of = a file whose=20 name I specify on the command line and then prompt the user for input = and print=20 that input. but the calls to gets are going to the file object opened by = IO.readlines. I can fix this with an explicit $stdin.get, but that's = kind of a=20 hack. I can also throw in this code:
 
while trash =3D = gets
end
 
and it consumes the file object from = IO.readlines=20 and lets me get on with reading from standard input. But again that's a = hack. Is=20 there some way to force gets() back to reading from standard input. i.e. = to=20 properly close out the file. I thought Using File.new and then explictly = closing=20 the object it returns would do that, but this code:
 
if fileName !=3D nil then
 
 inputFile  =3D open=20 fileName
 
 begin
  inputFile.each {|line|=20
  
   line.chomp!
   $l= ines=20 << lline
  }#end=20 inputFile.each
  
 ensure
 
  i= nputFile.close
 
 end #end = begin..ensure
 
end #if
has the same problem. Can someone tell = me what's=20 going on here. I'm totally lost. Thanks again
 
Jeremy Gregorio
rsilvergun@home.net
 
------=_NextPart_000_000B_01C16401.B84A3360--