May 27, 2015

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 = "ac_20150225"
  records = fyle.readlines
  counter =
  records.each do |record|
    pieces = record.split " "
    domain = pieces[1]
    if counter[domain] == nil then
      counter[domain] = 1
      counter[domain] = counter[domain] + 1
  p counter
  Sample Output
  trainee@kingston:~/lrp$ ruby toppers
  {""=>136408, ""=>591,
  ""=>59831, ""=>207,
  ""=>1040, ""=>1117,
  ""=>192, ""=>301, ""=>638,
  ""=>70, ""=>12,
  ""=>4, ""=>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].

Posted by gje at 07:21 AM | Comments (0)
More about Graham Ellis of Well House Consultants
Useful link: Ruby training
Related topics: via article database

May 26, 2015

Ruby - where one statement ends and the next begins

In Ruby, you can end your statements with a ;, but it's more usual to simply let them end at the end of a line. Ruby can usually identify where a statement ends and if it clearly doesn't end on one line, it carries on to the next.

Just occasionally, this can be little bit of a trap. Look at this code with teo apparently identicsl statements giving different results. Why?

  a = 5
  b = a
    + 6
  puts b
  b = a +
  puts b
  trainee@kingston:~/lrp$ ruby ld

The answer is that Ruby has split the first "statement" into two - whereas the second example clearly continued from one line to the next and was treated as a single statement.

It's great to be teaching Ruby this week - see our Ruby Courses ... and I'll be going on to Cucumber and Gherkin over the next couple of days.

Posted by gje at 07:00 PM | Comments (0)
Useful link: Ruby training
Related topics: via article database

May 25, 2015

Around the world from Melksham

Looking back over the last 18 months, I've used the new train service to or from places in no fewer that 12 different countries. How many can you identify?

Posted by gje at 06:33 PM | Comments (0)
Related topics: via article database

Well House Consultants Ltd.
Copyright 2015