From: Rick DeNatale Date: 2009-04-05T22:38:48+09:00 Subject: Re: parsing a file's contents --0016368e20f6f81ff80466cee89b Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Sun, Apr 5, 2009 at 2:32 AM, Andrew Timberlake < andrew@andrewtimberlake.com> wrote: > On Sun, Apr 5, 2009 at 5:04 AM, 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. > > > > Attachments: > > http://www.ruby-forum.com/attachment/3550/numbers.rb > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > Here's some code that does what you need: 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 <- 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 -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale --0016368e20f6f81ff80466cee89b--