# "chomping" an array at read time

**URL:** <https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958>\
**Category:** Ruby\
**Created:** [January 9, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958 "2006-01-09T18:47:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 9, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958/1 "2006-01-09T18:47:03Z")

</div>

Team,

ruby -v === ruby 1.8.2 (2004-12-25) [powerpc-aix4.3.3.0]

Please don’t laugh at my simplistic coding “techniques?”

I am reading a file which contains 1041 one token records into an array:

f\_lus = File.open("/file\_name", “r”)

lus = Array.new  
lus = File.read("/file\_name")

When I print the size of the array lus it shows: 14377.

So I figured that this has to do with the end of line chars at the end  
of  
each record/token. However, if you have 1 nl char per record the size  
should  
be twice the number of records or 2082 and not 14,377.

I went ahead and created a new array as follows, from the old array in  
the  
same script:

i = 0  
newArray = Array.new  
lus.each do |lu|  
newArray[i] = lu  
i += 1  
end

Now when I puts the size of array newArray it prints 1041 correctly.

What is happening here, please?  
Second, is there a way to chomp a record as it is read into the array?

Thank you

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 9, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958/2 "2006-01-09T18:56:07Z")

</div>

On Jan 9, 2006, at 9:44 AM, Victor R. wrote:

> f\_lus = File.open("/file\_name", “r”)
> 
> lus = Array.new  
> lus = File.read("/file\_name")

Right here you are createing a new arrau called lus. then you destroy  
it and overwrite it with a file handle. Please try this instead:

ezra:~ ez$ cat test.txt  
12  
324  
46  
23  
dgs  
fh  
asf  
fh  
dsg

lus = File.open(“test.txt”) {|f| f.readlines}

p lus  
#=\> [“12\n”, “324\n”, “46\n”, “23\n”, “dgs\n”, “fh\n”, “asf\n”, “fh  
\n”, “dsg\n”]

Cheers-  
Ezra

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 9, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958/3 "2006-01-09T19:02:10Z")

</div>

On Jan 9, 2006, at 11:53 AM, Ezra Z. wrote:

> lus = File.open(“test.txt”) {|f| f.readlines}

Or just:

lus = File.readlines(“test.txt”)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 9, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958/4 "2006-01-09T19:29:23Z")

</div>

On Jan 9, 2006, at 9:59 AM, James Edward G. II wrote:

> On Jan 9, 2006, at 11:53 AM, Ezra Z. wrote:
> 
> > lus = File.open(“test.txt”) {|f| f.readlines}
> 
> Or just:
> 
> lus = File.readlines(“test.txt”)

James-

```
When you do that does it close the file handle as well? I was just

```

using the block to make sure the file handle gets closed. Are there  
certain actions that will close the handle by themselves and others  
that won’t?

Thanks-  
-Ezra

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 9, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958/5 "2006-01-09T21:57:25Z")

</div>

Thank you both.

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [January 10, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/chomping-an-array-at-read-time/51958/6 "2006-01-10T20:55:56Z")

</div>

On Jan 9, 2006, at 12:28 PM, Ezra Z. wrote:

> James-
> 
> When you do that does it close the file handle as well? I was just  
> using the block to make sure the file handle gets closed. Are there  
> certain actions that will close the handle by themselves and others  
> that won’t?

Well, there’s no way I know of to get at the file handle used here,  
so I sure hope it’s closed. I would argue that it’s a bug if it  
isn’t. 🙂

James Edward G. II
