-   Programming (
-   -   cleaning the readability of this ruby script (

hedpe 12-25-2006 08:49 PM

cleaning the readability of this ruby script
Hey guys,

I could really use some help cleaning the readability of this ruby script. I could not figure out how to read files more properly for my situation. I know there is a fh.each_line ... but I'm reading more into blocks within the file... first here is the script: (md5sum 1742e0c0fc2022ca06cd99f07342d1c3)


        ip =

        ARGV.each do |dir|
                files =                                # Going to get all of the files in this dir which is all of the
                files = Dir.glob("#{dir}/**/*")                # 5 minute intervals in the day

                files.each do |file|                # for each file get the number of connections
                        next if
                        puts "#{file}"
                        fh =, "r")                # open the file for reading
                        fh.readline                # skip the first four lines

                                ipaddr = fh.readline                # the first line of the block is the IP address
                                ip[ipaddr] = if(!ip[ipaddr])

                                while(1)        # while we are inside the block of IP's they had contact with
                                        (dir, contact, count, loc) = fh.readline.split        # read the line in and split it
                                        break if(dir=="IP:")                # "IP:" denotes the end of the block
                                        ip[ipaddr][contact]=1                # The value doesn't matter, as long as the key exists

                                        fh.readline                                                #        skip blank line after block
                                        break if(fh.eof?)                                # go to the next file if EOF

        of ="degree_timespan", "w")
        ip.each do |key, value|
                of.puts "#{key.chop} #{ip[key].length}"


To test it create a directory called "test" and put these two files in it:


ce0fc04cd448d645f0e1a259a4b40d16  /var/www/localhost/htdocs/test1
5532b17a93b51fbb5abd5b640245af8e /var/www/localhost/htdocs/test2

Then you can run it, and check your output with mine:

gnychis@x60s ~/school/thesis/host_analysis/degree $ ./unique_contact.rb test
gnychis@x60s ~/school/thesis/host_analysis/degree $ cat degree_timespan 3 1 2

There must be a better way to reading in the file than the code I wrote.

To help you better understand the input:
Code: *** TOP LEVEL HASH KEY ***
        <- 1 (inter)  *** HOST IT HAS CONTACTED ***
        <- 1 (inter)  *** HOST IT HAS CONTACTED ***
IP: RA_Out: 0 ER_Out: 0 RA_In: 0 ER_In: 1 RA: 0 ER: 1 Total: 1  ** KNOW THE BLOCK ENDED*


archtoad6 12-26-2006 03:46 PM

What do you mean by "readability"?

To me, it's the layout of the code on the page in a way that makes the logic easy to see.

However, "fh.each_line ... but I'm reading more into blocks within the file... " makes me think you have something else in mind.

Some things you could do that would improve my kind of "readability":
  • Ruthlessly limit line length to 72 characters, just like the old punch cards (the last 8 columns were often a serial/line # -- in case the deck got dropped).
  • Change your indent to 3 spaces rather than a tab.
  • Line up your comments in more regular columns.

Right now your script is a major pain to read because of the horizontal scrolling it causes.

All times are GMT -5. The time now is 06:45 PM.