First post

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

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 D.
Jane Doe
Donald Duck
Donald Notreal
Mike M.s

The threadz.txt reads as so:

John D. 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 D. 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 M.s March 9 at 4:52pm Reply
aye… dont give a shit really

I would like to learn how to make this program grab the first name in
the array, use it to search the thread.txt for posts using the name as
a delimiter?
I want to have it return results for:
how many posts?
who posted the most?
who posted the least?
who said the most bad words?
Searching for text.
How can I teach my program to recognize posts?
Can the date and time be manipulated or used to my advantage?
I am currently using “Beginning Ruby” by Peter C…
Here is the ugly code… sorry once again.

--------------------------------------------------------------------------------///////////////ThreadReader1.0\\\\\\\\\\\\\

class Texfun

           #Names Array values = 0 - 20
   def namez
           $fbNamez = []
           nf = File.open("names.txt", "r")
           nf.each_line { |line| $fbNamez.push line }
           nf.close
           puts $fbNamez
           end

#Thread Array Values 0-1913~~
def threadz
$fbThreadz = []
tf = File.open(“thread.txt”, “r”)
tf.each_line { |line| $fbThreadz.push line}
tf.close
puts $fbThreadz
end

#Number of Posts
def post_counter
text = $fbThreadz.join
total_posts = “Total Number of posts:
#{text.split(“Reply”).length}”
end

#name finder
def look
e = gets.to_i
puts $fbNamez
end

end

printer = Texfun.new
printer.look

Any insight would be appreciated.
Thanks in advance.

On Wed, Apr 7, 2010 at 6:02 PM, Alexander Santoro <
[email protected]> wrote:

          puts $fbNamez
          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.

John D. March 9 at 4:18pm Reply
Mike M.s 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 D. 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