Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-25-2006, 07:49 PM   #1
Registered: Jan 2005
Location: Pittsburgh
Distribution: Ubuntu
Posts: 378

Rep: Reputation: 30
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*

Last edited by hedpe; 12-25-2006 at 07:53 PM.
Old 12-26-2006, 02:46 PM   #2
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Subversion ruby script fails fenrisW0lf *BSD 1 12-21-2007 06:57 PM math PDF conversion or readability skoot Linux - Desktop 0 08-29-2006 02:44 AM
Readability of posts cwwilson721 General 36 05-21-2006 04:18 PM
Cleaning up an old PC Merlyn Linux - Hardware 8 10-25-2005 06:33 AM
cleaning up? phonecian Linux - Software 1 10-19-2003 06:27 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration