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
if counter[domain] == nil then
counter[domain] = 1
counter[domain] = counter[domain] + 1
trainee@kingston:~/lrp$ ruby toppers
"melksh.am"=>192, "twhc.org.uk"=>301, "www.wellhousemanor.co.uk"=>638,
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 articlesR106 - Input and Output in Ruby 
Expect with Ruby - a training example to get you started - (2016-05-18) 
Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09) 
Formatting your output - options available in Ruby - (2010-09-29) 
Exclamation marks and question marks on ruby method names - (2010-07-28) 
Ruby collections and strings - some new examples - (2010-02-03) 
Neatly formatting results into a table - (2010-02-01) 
Opening and reading files - the ruby fundamentals - (2009-07-16) 
Ruby Programming Course - Saturday and Sunday - (2008-11-16) 
Some Ruby programming examples from our course - (2008-03-21)R107 - Collections (Arrays and Hashes) in Ruby 
Reading and parsing a JSON object in Ruby - (2015-06-01) 
Shuffling a list - Ruby and Python - (2014-12-28) 
Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09) 
Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12) 
All possible combinations from a list (Python) or array (Ruby) - (2011-04-23) 
Process every member of an array, and sort an array - Ruby - (2011-04-21) 
Is this number between? Does this list include? - Ruby - (2011-04-18) 
Creating, extending, traversing and combining Ruby arrays - (2010-09-30) 
What are Ruby Symbols? - (2010-02-02) 
Sorting arrays and hashes in Ruby - (2010-01-30) 
Collection objects (array and hash) in Ruby - (2009-07-16) 
Adding a member to a Hash in Ruby - (2006-12-16)
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