With delegates on public Well House Consultants courses staying in our on-site accommodation, there's time for them to learn a lot more about each other's applications and get a far wider view of the uses of the subject they're with us to learn. There's time for the delegate who wants to extend practicals to tryout some more code, and for delegates who would like the tutor to show them further examples.
On Friday, I was asked for further examples to illustrate polymorphism, and regular expressions in ruby, and to do so in an example which related to some practical use. Also to show a test harness / test cases in use.
The first examples -
(see source) was a class for handling email addresses - with a test harness supplied too. The email addresses are initially split down into a user name and a domain, and then the domain is split down further into each domain level - so graham.ellis@sheepbingo.co.uk is split down to graham.ellis and sheepbingo.co.uk - which is then split down to sheepbingo, co and uk. This is a good reminder to not get involved with
Matcho matching where the programmer tries to solve the whole problem with a single command / regular expresion!
I also wrote a server log analysis application - the regular expression was more complex here - and used four different classes for each server access; that's because I want to treat a success as a slightly different record to a failure, and split the failures between whether the failure was due to a server error or an error in the request. Of course, I didn't write four completely different classes - I simply wrote a single base class and then provided other classes that inherited from it, with only tiny bits of code in the subclasses which indicate the changes. See
[here]. An extended version of that application -
[here] - goes on to show how these classes of server access object are used and sorted to produce a list of the pages on our web site that were accessed in the last 24 hours, how many times, and from where.
P.S. These latter examples are interesting in that they also use a wholesale factory method - a static or class method which returns a whole array of new objects. There's more about factory methods
[here].
(written 2010-10-02, updated 2013-01-01)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R109 - Ruby - Strings and Regular Expressions [970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
[986] puts - opposite of chomp in Ruby - (2006-12-15)
[987] Ruby v Perl - interpollating variables - (2006-12-15)
[1195] Regular Express Primer - (2007-05-20)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1588] String interpretation in Ruby - (2008-03-21)
[1875] What are exceptions - Python based answer - (2008-11-08)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1891] Ruby to access web services - (2008-11-16)
[2293] Regular Expressions in Ruby - (2009-07-16)
[2295] The dog is not in trouble - (2009-07-17)
[2608] Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31)
[2614] Neatly formatting results into a table - (2010-02-01)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2623] Object Oriented Ruby - new examples - (2010-02-03)
[3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
[3621] Matching regular expressions, and substitutions, in Ruby - (2012-02-23)
[3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
[3758] Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09)
[4388] Global Regular Expression matching in Ruby (using scan) - (2015-01-08)
[4505] Regular Expressions for the petrified - in Ruby - (2015-06-03)
[4549] Clarrissa-Marybelle - too long to really fit? - (2015-10-23)
R108 - Ruby - More Classes and Objects [184] MTBF of coffee machines - (2005-01-20)
[656] Think about your design even if you don't use full UML - (2006-03-24)
[1217] What are factory and singleton classes? - (2007-06-04)
[1587] Some Ruby programming examples from our course - (2008-03-21)
[2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
[2601] Ruby - is_a? v instance_of? - what is the difference? - (2010-01-27)
[2603] Ruby objects - a primer - (2010-01-29)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
[2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
[3142] Private and Public - and things between - (2011-01-22)
[3154] Changing a class later on - Ruby - (2011-02-02)
[3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
[3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
[3760] Why you should use objects even for short data manipulation programs in Ruby - (2012-06-10)
[3781] Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby? - (2012-06-23)
[3782] Standard methods available on all objects in Ruby - (2012-06-23)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
[4504] Where does Ruby load modules from, and how to load from current directory - (2015-06-03)
[4550] Build up classes into applications sharing data types in Ruby - (2015-10-23)
[4551] Testing your new class - first steps with cucumber - (2015-10-23)
Some other Articles
Customer Review - Hotel Room, MelkshamStrong arm tactics in the bus industry? Poor result for the customer!Travel, Transport and Tourism - A vision for Melksham, 2026How to set up short and meaningfull alternative URLsRuby - examples of regular expressions, inheritance and polymorphism Ruby - yield; parallel routinesChristmas 2010 - Well House Manor, Melksham, HotelCreating, extending, traversing and combining Ruby arraysWhy do I need brackets in Ruby ... or Perl, Python, C or Java