From: Josh Cheek Date: 2010-04-08T09:56:06+09:00 Subject: Re: First post --000e0cd11ae0b34bf60483af24db Content-Type: text/plain; charset=ISO-8859-1 On Wed, Apr 7, 2010 at 6:02 PM, Alexander Santoro < alexandersantoro@gmail.com> wrote: > Hello I am new to Ruby and programming. I hate to just dump code here > so if you don't feel like responding I understand. I am trying to > import two text files, one containing names and one containing text. > I did this using : > --------------------------------------------------------------------------- > def namez > $fbNamez = [] > nf = File.open("names.txt", "r") > nf.each_line { |line| $fbNamez.push line } > nf.close > puts $fbNamez > end > > ----------------------------------------------------------------------------- > > def threadz > $fbThreadz = [] > tf = File.open("thread.txt", "r") > tf.each_line { |line| $fbThreadz.push line} > tf.close > puts $fbThreadz > end > > ------------------------------------------------------------------------------ > Variables that begin with $ are global variables. Using globals is frowned upon, because it makes the flow of control very difficult to follow. A better approach would be to use local variables, and have the function return the results (by default, the function will always return whatever the last line evaluates to) The file open and close thing is also not the best way to do this, a better approach would be to open the file and pass it a block, then let it pass back the file variable. This will ensure that the file gets closed correctly, and I think makes the code quite a bit cleaner. The indentation conventions you've used aren't standard for Ruby. They won't break anything, but people are likely to be irritated by it. Usually, try to indent two spaces. Ruby naming conventions have local variables as snake_case rather than camelCase, so fbThreadz should be fb_threadz So you might rewrite like this. def namez fb_namez = Array.new File.open( "names.txt", "r" ) do |nf| nf.each_line { |line| fb_namez.push line } end puts fb_namez fb_namez end But, there is fortunately an even easier way than this. You can use the readlines method to pull out all the lines def namez fb_namez = File.readlines("names.txt") puts fb_namez fb_namez end fb_namez at the end there just returns our results. But really, this function shouldn't be printing these names out, that is a very specific job that makes the method useless to anyone else who wants to get a list of names but not print them. Whoever calls this method should be the one printing the names. Which means we have no need for this fb_namez variable at all. So then we can just do def namez File.readlines "names.txt" end puts namez I know this cannot be the best way to do this but it was all I muster. > The goal was to iterate through the array of names and extrapolate > data relative to the thread.txt file. Example: The names.txt file > reads as so: > > John Doe > Jane Doe > Donald Duck > Donald Notreal > Mike Mikes > > > > The threadz.txt reads as so: > > -------------------------------------------------------------------------------- > John Doe March 9 at 4:09pm Reply > ok so who likes steph more than Sonia..... cuz i do > > Jane Doe March 9 at 4:13pm Reply > hahahahah what aaaan asshole > > John Doe March 9 at 4:18pm Reply > hhhhhhhhhhhhhhhhhhhhhhaaaaaaaaaaa > > Donald Duck March 9 at 4:50pm Reply > seriously, lets take a vote > > all in favor say I > > Donald Notreal March 9 at 4:51pm Reply > aye > > Mike Mikes March 9 at 4:52pm Reply > aye.. dont give a shit really > > ------------------------------------------------------------------------------ > > Unfortunately I think you are going to run into issues when you go to delimit your file. How do you know where one post ends and the next begins? What if someone makes a post that quotes someone else? For example, what if John Doe quotes Jane doe and his post looks like this "I can't believe Jane said: Jane Doe March 9 at 4:13pm Reply hahahahah what aaaan asshole" If you are creating this file, then I think you should put in specific delimiters, so that you can easily tell when a new post starts. Also, just so you know, calling your array Threadz is kind of confusing, because when someone reads something like text = $fbThreadz.join They are probably going to think you are Ruby's Thread class, http://ruby-doc.org/core/classes/Thread.html#M000462 --000e0cd11ae0b34bf60483af24db--