A little program can do a LOT of work! Scenario - I have a web server log file of some 50 Mbytes (the data from one particular day on our server) and the secon field in each line tells me which of our hosted web sites was being visited. The question I was asked - "how many hits on each host?". Solution - in Ruby:
# Program to count server acceses to each virtual host
fyle = File.new "ac_20150225"
records = fyle.readlines
counter = Hash.new
records.each do |record|
pieces = record.split " "
domain = pieces[1]
if counter[domain] == nil then
counter[domain] = 1
else
counter[domain] = counter[domain] + 1
end
end
p counter
__END__
Sample Output
trainee@kingston:~/lrp$ ruby toppers
{"www.wellho.net"=>136408, "www.savethetrain.org.uk"=>591,
"www.firstgreatwestern.info"=>59831, "www.across-the-pond.co.uk"=>207,
"www.twcrp.org.uk"=>1040, "www.melkshamchamber.org.uk"=>1117,
"melksh.am"=>192, "twhc.org.uk"=>301, "www.wellhousemanor.co.uk"=>638,
"transwilts.org.uk"=>70, "railcustomer.info"=>12,
"thebutlerdidit.info"=>4, "www.consultations.org.uk"=>3}
As a quick and dirty single time solution, I didn't implement this using objects, and I allowed myself to use the built in
p function to display the data in a quick but crude and effective way when it had been gathered.
I'm presenting a
Ruby Training Course this week and we've been discussiong how books and web sites seem to go straight from "hello world" to complicated examples. That's because the pubication of quick, dirty examples that don't validate data - such as the one above - often lead to criticism from readers taking them out of context and down-marking them / slagging them off in public because of their dirtyness.
In the interest of providing my readers with an intermediate step, I'm risking the wrath of these out-of-context readers in publishing the above. .... if you've been on one of our courses, you'll know what I'm doing. If you haven't, and are uncomfortable with the above, please come along - see
[here] for list of currently scheduled public courses.
The complete source of the example above is
[here].
(written 2015-05-27, updated 2015-05-28)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R107 - Collections (Arrays and Hashes) in Ruby [991] Adding a member to a Hash in Ruby - (2006-12-16)
[2291] Collection objects (array and hash) in Ruby - (2009-07-16)
[2606] Sorting arrays and hashes in Ruby - (2010-01-30)
[2618] What are Ruby Symbols? - (2010-02-02)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2976] Creating, extending, traversing and combining Ruby arrays - (2010-09-30)
[3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
[3255] Process every member of an array, and sort an array - Ruby - (2011-04-21)
[3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
[3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
[3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
[4368] Shuffling a list - Ruby and Python - (2014-12-28)
[4502] Reading and parsing a JSON object in Ruby - (2015-06-01)
R106 - Input and Output in Ruby [1587] Some Ruby programming examples from our course - (2008-03-21)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[2290] Opening and reading files - the ruby fundamentals - (2009-07-16)
[2614] Neatly formatting results into a table - (2010-02-01)
[2893] Exclamation marks and question marks on ruby method names - (2010-07-28)
[2974] Formatting your output - options available in Ruby - (2010-09-29)
[3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)
[4678] Expect with Ruby - a training example to get you started - (2016-05-18)
Some other Articles
Separating your code for easier testing, understanding and re-use; example in RubyDefining the behaviour of your web site and testing that it worksThe TransWilts Community Intergrated Transport CorridorSignificant work - beyond helloworld in RubyRuby - where one statement ends and the next beginsAround the world from MelkshamSunday train times - 17th May to 6th September 2015Swindon to Westbury train services - Saturdays from 23rd May 2015Monday to Friday / TransWilts train times from 18 May to 11th December 2015