From: "David A. Black" Date: 2008-12-25T02:26:48+09:00 Subject: Re: newbie qusetion: read a file and print out same file Hi -- On Thu, 25 Dec 2008, joemacbusiness@gmail.com wrote: > Hi All, > > How do I read an input file and print it back to STDOUT > exactly as the original file. This should be very simple > but the output of my program is all garbled. > What am I doing wrong? > ( I am using http://www.rubycentral.com/book to learn Ruby) > > The program, input, and runtime output are below. > > Thanks, --JM > ================= snip ============== > > $ cat ./test56.rb > #!/usr/local/bin/ruby > > inputFile = File.readlines("input4", 'r') > > for item in inputFile > item.chop() > print item, "\n" > end You're working too hard :-) input_file = File.readlines("input4") puts input_file or just puts File.read("input4") If you do have occasion to loop through an array of lines (which no doubt you will at some point), remember that chop doesn't do a permanent chop. What you probably want is chomp!, which removes a trailing newline if there is one. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)