# Newbie qusetion: read a file and print out same file

**URL:** <https://www.ruby-forum.com/t/newbie-qusetion-read-a-file-and-print-out-same-file/156335>\
**Category:** Ruby\
**Created:** [December 24, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/newbie-qusetion-read-a-file-and-print-out-same-file/156335 "2008-12-24T18:10:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 24, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/newbie-qusetion-read-a-file-and-print-out-same-file/156335/1 "2008-12-24T18:10:26Z")

</div>

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](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  
$  
$ cat input4  
car  
tree  
house  
candy bar  
paper  
ocean  
$ ./test56.rb  
car

tr  
ee  
house  
candy bar

paper

ocean

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 24, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/newbie-qusetion-read-a-file-and-print-out-same-file/156335/2 "2008-12-24T18:36:46Z")

</div>

Hi –

On Thu, 25 Dec 2008, removed\_email\_address@domain.invalid wrote:

> Thanks, --JM  
> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 24, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/newbie-qusetion-read-a-file-and-print-out-same-file/156335/3 "2008-12-24T21:05:55Z")

</div>

On Dec 24, 9:26 am, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > What am I doing wrong?  
> > inputFile = File.readlines(“input4”, ‘r’)
> 
> –  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training:[http://www.rubypal.com](http://www.rubypal.com)  
> Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

Hello David,

I wanted to loop through the input file “line-by-line”  
and print it out the same way. Sorry I did not make that  
clear.  
Here is the code block that does what I wanted.

inputFile = File.open(“input4”, ‘r’)  
while line = inputFile.gets()  
puts line  
end

Thank you, --JM

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 25, 2008, 11:44am UTC](https://www.ruby-forum.com/t/newbie-qusetion-read-a-file-and-print-out-same-file/156335/4 "2008-12-25T11:44:22Z")

</div>

unknown wrote:

> $ cat input4  
> car  
> tree  
> house  
> candy bar  
> paper  
> ocean  
> $ ./test56.rb  
> car
> 
> tr  
> ee  
> house  
> candy bar
> 
> paper
> 
> ocean

Interesting output, especially the split ‘tree’. To see what’s going on,  
I added an extra line to your code:

inputFile = File.readlines(“input4”, ‘r’)  
p inputFile

This gives the following output:

[“car”, “\ntr”, “ee\nhouse\ncandy bar”, “\npaper”, “\nocean\n”]

To understand what’s happening here, read the documentation under “ri  
IO::readlines”

## ---------------------------------------------------------- IO::readlines IO.readlines(name, sep\_string=$/) =\> array

```
 Reads the entire file specified by _name_ as individual lines, and
 returns those lines in an array. Lines are separated by
 _sep_string_.

    a = IO.readlines("testfile")
    a[0] #=> "This is line one\n"

```

So, the problem is that the ‘r’ is not the file input mode, but the  
separator character! The fact you had the words “car”, “bar” and “paper”  
(all ending in ‘r’) made this rather unclear. It looks like it’s mostly  
splitting at the end of a line, but actually it’s splitting after a  
letter ‘r’.
