Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Significant work - beyond helloworld in Ruby

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
R106 - Input and Output in Ruby
  [4678] Expect with Ruby - a training example to get you started - (2016-05-18)
  [3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)
  [2974] Formatting your output - options available in Ruby - (2010-09-29)
  [2893] Exclamation marks and question marks on ruby method names - (2010-07-28)
  [2621] Ruby collections and strings - some new examples - (2010-02-03)
  [2614] Neatly formatting results into a table - (2010-02-01)
  [2290] Opening and reading files - the ruby fundamentals - (2009-07-16)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [1587] Some Ruby programming examples from our course - (2008-03-21)

R107 - Collections (Arrays and Hashes) in Ruby
  [4502] Reading and parsing a JSON object in Ruby - (2015-06-01)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
  [3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3255] Process every member of an array, and sort an array - Ruby - (2011-04-21)
  [3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
  [2976] Creating, extending, traversing and combining Ruby arrays - (2010-09-30)
  [2618] What are Ruby Symbols? - (2010-02-02)
  [2606] Sorting arrays and hashes in Ruby - (2010-01-30)
  [2291] Collection objects (array and hash) in Ruby - (2009-07-16)
  [991] Adding a member to a Hash in Ruby - (2006-12-16)


Back to
Ruby - where one statement ends and the next begins
Previous and next
or
Horse's mouth home
Forward to
The TransWilts Community Intergrated Transport Corridor
Some other Articles
Separating your code for easier testing, understanding and re-use; example in Ruby
Defining the behaviour of your web site and testing that it works
The TransWilts Community Intergrated Transport Corridor
Significant work - beyond helloworld in Ruby
Ruby - where one statement ends and the next begins
Around the world from Melksham
Sunday train times - 17th May to 6th September 2015
Swindon to Westbury train services - Saturdays from 23rd May 2015
Monday to Friday / TransWilts train times from 18 May to 11th December 2015
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4499_Sig ... -Ruby.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat