# Parsing a file's contents

**URL:** <https://www.ruby-forum.com/t/parsing-a-files-contents/164743>\
**Category:** Ruby\
**Created:** [April 5, 2009, 5:05am UTC](https://www.ruby-forum.com/t/parsing-a-files-contents/164743 "2009-04-05T05:05:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyred1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rubyred1](https://www.ruby-forum.com/u/rubyred1)\
**Post date:** [April 5, 2009, 5:05am UTC](https://www.ruby-forum.com/t/parsing-a-files-contents/164743/1 "2009-04-05T05:05:19Z")

</div>

Hello all, I’m relatively new to ruby and this forum so I’m not sure I’m  
posting this in the right area but here goes. My problem is I have a  
file I want to read in, the file contains numbers in a CSV set-up like  
so: 2, 3, 15, 25, 29 the numbers are all written out like that 5 per  
row. What I want to do is split the numbers into columns that the 2 and  
the number under it are all in an array. The main problem I’m having is  
that when I try to put the numbers into the array I get a NoMethodError  
for the String type which I don’t understand since I created the every  
method for the Array class not String. I attached the code any  
assistance is appreciated.

---

<div class="post-metadata">

**Author:** ![rubyred1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rubyred1](https://www.ruby-forum.com/u/rubyred1)\
**Post date:** [April 5, 2009, 8:34am UTC](https://www.ruby-forum.com/t/parsing-a-files-contents/164743/2 "2009-04-05T08:34:22Z")

</div>

On Sun, Apr 5, 2009 at 5:04 AM, Ant 23 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Attachments:  
> [http://www.ruby-forum.com/attachment/3550/numbers.rb](http://www.ruby-forum.com/attachment/3550/numbers.rb)
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Here’s some code that does what you need: [gist:90389 · GitHub](http://gist.github.com/90389)

A quick look through your code with some comments:

# ← This is completely unecessary

class Array  
def every(n)  
select{|x| index(x) % n == 0}  
end  
end

# ← The way you are writing this makes this class completely

unecessary. For this simple use-case, it’s okay to write it as a  
simple script.  
class Numbers

#Declare the arrays that will hold the numbers when they are seperated  
colA = []  
colB = []  
colC = []  
colD = []  
colE = []

#Declare the array that will hold all the numbers in the file ←   
Ruby will fill this with a string because that’s what each line of the  
file will be read as  
allNumbers = []  
#now we just have to get the numbers from the file into the array ←   
No need for a counter  
counter = 1  
File.open(“C:\Documents and Settings\Desktop\test.txt”, “r”) do  
|infile|  
while(allNumbers = infile.gets) ← This is where ruby will change  
allNumbers to a String  
#this is not working as ruby is taking the files contents as strings  
not ints ← You got it  
#colA = allNumbers.every 5 #in theory should take every fifth  
number from allNumbers and put it in colA  
puts “#{allNumbers.to\_s}” #ok it works but now i need to put the  
numbers into each different array ← It works because you’re printing  
out the line from the file  
colA = allNumbers.every 5 #in theory should take every fifth number  
from allNumbers and put it in colA ← Won’t work because you’re  
expecting an array  
puts “#{colA.to\_s}” #print out the numbers in colA  
counter = counter + 1 #why aint this counter++ ← Because  
that’s not ruby, you can do counter += 1  
end  
end  
end

It looks like you’re coming from a Java background. Ruby tends to do  
things quite differently even though it can look similar in some  
respects.  
Here’s an online book that could be useful to get a quick overview of  
Ruby -  
[http://www.ruby-doc.org/docs/ruby-doc-bundle/ProgrammingRuby/index.html](http://www.ruby-doc.org/docs/ruby-doc-bundle/ProgrammingRuby/index.html)

Hope that helps

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![rubyred1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rubyred1](https://www.ruby-forum.com/u/rubyred1)\
**Post date:** [April 5, 2009, 3:43pm UTC](https://www.ruby-forum.com/t/parsing-a-files-contents/164743/3 "2009-04-05T15:43:19Z")

</div>

# while( allNumbers = infile.gets)

infile.gets returns a “string” that contains a line of the file.

I guess that you want this line of code to extract the numbers in the  
file and put them into the allNumbers array. Remember that gets only  
reads one line in each iteration.

you can do it simply with

– while ( allNumbers = infile.gets.scan(/\d+/) )

#scan extracts the numbers from the string and puts them into the  
allNumbers array.

# now with each iteration allNumbers contains the numbers in that line.

# if you want to put fifth number in the colA array you can do it like

this.

– while ( allNumbers = infile.gets.scan(/\d+/) )  
– colA.push(allNumbers[4]) # fifth number in allNumbers[4]

---

<div class="post-metadata">

**Author:** ![rubyred1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rubyred1](https://www.ruby-forum.com/u/rubyred1)\
**Post date:** [April 5, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/parsing-a-files-contents/164743/4 "2009-04-05T15:41:40Z")

</div>

On Sun, Apr 5, 2009 at 2:32 AM, Andrew T. \<  
removed\_email\_address@domain.invalid\> wrote:

> > assistance is appreciated.
> 
> simple script.  
> class Numbers
> 
> #Declare the arrays that will hold the numbers when they are seperated  
> colA = []  
> colB = []  
> colC = []  
> colD = []  
> colE = []
> 
> #Declare the array that will hold all the numbers in the file ←

Snippage deleted.

> It looks like you’re coming from a Java background. Ruby tends to do  
> things quite differently even though it can look similar in some  
> respects.

Yes, the comments about “declaring” variables in Ruby indicates a bad  
transfer of knowledge from some other language. Ruby doesn’t HAVE  
declarations, variables are simply references to objects, and can be  
changed  
over time to whatever the program causes them to be changed to.

[http://talklikeaduck.denhaven2.com/2006/09/13/on-variables-values-and-objects](http://talklikeaduck.denhaven2.com/2006/09/13/on-variables-values-and-objects)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![rubyred1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rubyred1](https://www.ruby-forum.com/u/rubyred1)\
**Post date:** [April 5, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/parsing-a-files-contents/164743/5 "2009-04-05T18:54:03Z")

</div>

Andrew T. wrote:

> It looks like you’re coming from a Java background. Ruby tends to do  
> things quite differently even though it can look similar in some  
> respects.  
> Here’s an online book that could be useful to get a quick overview of  
> Ruby -  
> [http://www.ruby-doc.org/docs/ruby-doc-bundle/ProgrammingRuby/index.html](http://www.ruby-doc.org/docs/ruby-doc-bundle/ProgrammingRuby/index.html)
> 
> Hope that helps
> 
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain

yes I mostly work in java so ruby is kind of new to me, although there  
are some similarities, I notice that I am writing ruby like I’m writing  
in java and thats when the differences show up. Thanks for the link I’m  
am going to study it and thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![rubyred1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@rubyred1](https://www.ruby-forum.com/u/rubyred1)\
**Post date:** [April 5, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/parsing-a-files-contents/164743/6 "2009-04-05T16:56:00Z")

</div>

Ant 23 wrote:

> Hello all, I’m relatively new to ruby and this forum so I’m not sure I’m  
> posting this in the right area but here goes. My problem is I have a  
> file I want to read in, the file contains numbers in a CSV set-up like  
> so: 2, 3, 15, 25, 29 the numbers are all written out like that 5 per  
> row. What I want to do is split the numbers into columns that the 2 and  
> the number under it are all in an array. The main problem I’m having is  
> that when I try to put the numbers into the array I get a NoMethodError  
> for the String type which I don’t understand since I created the every  
> method for the Array class not String. I attached the code any  
> assistance is appreciated.

cols = []  
row = 0  
file.each\_line do |line|  
line.chomp.split(/\s\*,\s\*/).each\_with\_index do |num, col|  
cols[col] ||= []  
cols[col][row] = num.to\_i rescue nil  
end  
row += 1  
end

Now cols[0] contains the first column, and so on.
