Exercises, examples and other material relating to training module R108. This topic is presented on public courses
Beyond the basic classes, you'll wish
to use the power of interitance, encapsulation
and polymorphism. Don't worry if these are
new terms to you - they're explained in this
|Articles and tips on this subject
|Testing your new class - first steps with cucumber
A recent article ([here]) showed you how we build up a series of classes, each with their own piece of test code, into applications.
It's all very well putting a piece of test code in the bottom of our class definition file - and that's a useful thing to do - but it's just a single test, and it doesn't ...
|Build up classes into applications sharing data types in Ruby
I've put together a "build-up" example showing code shared between multiple applications and using multiple classes in Ruby.
Start with stations.rb which defines a railway station object and includes a factory to load a Hash of all UK stations. There's also a test program in there so that it can run ...
|Where does Ruby load modules from, and how to load from current directory
All (scripting) languages allow you to load code from other supporting source files, using keywords like use, load, source and include. In Ruby, the most common way to load other code is to require it - and if you use the require method you'll load in code from another file which will be assumed to ...
|Changing what operators do on objects - a comparison across different programming languages
In Object Oriented languages, you used named pieces of code known as methods to perform an operation on a variable - for example you might write
to take a list of (something) for a week and add a list relating to weekend onto the end of it.
Sometimes, the method ...
|Ruby - examples of regular expressions, inheritance and polymorphism
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 ...
|Standard methods available on all objects in Ruby
There are a lot of things I can do with any object in Ruby - there's a whole package deal of methods available to me by just defining and creating an object:
irb(main):003:0> class Thing
irb(main):005:0> widget = Thing.new
|Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby?
There's no requirement for you to use the words "public", "private" and "protected" in Ruby - methods within classes that you write default to being public anyway. But you may wish to protect certain of your methods from being called directly by the users of your classes.
So - what protection do they ...
|Why you should use objects even for short data manipulation programs in Ruby
It's so easy to take the "Bull at a gate" approach - to start writing code to do the job in hand without giving thought to code-reuse later on, or indeed to how the work can be used again later. And it's double easy to, for me to take the bull approach during a course, where I'm writing a short demonstration ...
|Ruby - a training example that puts many language elements together to demonstrate the whole
Towards the end of our programming language training courses, we pull together all the various strands into a worked example that shows how they go together. I've just posted such an example from last week's Ruby Programming Course ... [here].
Let's have a look at some of the things in the example ...
|Ruby training - some fresh examples for string handling applications
Ruby's a great language. No - let me rephrase that "Ruby's a fantastic language" ... for many tasks such as "data munging" - handling / manipulating large flows of data, in all sorts of ways. But in my training role, I come across far more people using Ruby on Rails, Selenium and Watir than Ruby for ...
|Changing a class later on - Ruby
In Ruby, you can define a class ... and then come back and add methods to it. But why would you want to?
Let's suppose that you've got a base class - I'll use "Rectangle" as my example, and you've already subclassed it to "Square" and perhaps a few other things, via a required file that you share between ...
|Private and Public - and things between
A public train service is one which is available for anyone to travel on; a private one only takes limited passengers as invites / made available by the operator. And there could be intermediate levels too - I understand that at times the British Transport Police protect the last train from Weymouth ...
|What is a factory method and why use one? - Example in Ruby
How do you create an object? By calling a constructor method, of course.
But ... actually ... there's no "of course" about it. Here's a piece of code from an example I wrote yesterday:
and that call creates an object ...
Whilst you can create ...
|The Multiple Inheritance Conundrum, interfaces and mixins
Should an OO programming language support "multiple inheritance"? Let's define multiple inheritance first - starting from simple (single?) inheritance.
I don't want to have to define each type of thing ("class of object") from scratch, so I'll define once class as being based ...
|Direct access to object variable (attributes) in Ruby
Rather than writing getters and setters, in many Object Oriented languages you can access the variables within an object directly. That can be dangerous for code flexibility for the future, as it means that you're removing the possibility of interspersing code. However, it can be very convenient.
|Defining a static method - Java, Python and Ruby
Most methods in classes that your write will be run on / applied to individual objects within that class - you'll be asking for the colour of a marker pen, or setting the price of a hotel room. You will NOT - typically - have a model in which all marker pens share the same colour.
But - just occasionally ...
|Tips for writing a test program (Ruby / Python / Java)
Where does my test code go?
If you've written a class - a series of methods to be used within another application - how do you test it? How about writing a test program within the same file which runs as the main program if you run your class on its own from the command line, but is ignored if you ...
|Ruby objects - a primer
If you're new to Ruby, but familiar with Object Oriented Programming, you might like to take a quick look at the example that I wrote during yesterday's course which shows how all the major OO elements are implemented in Ruby. The sample source is [here].
Here are some of the key facts:
• Classes ...
|Ruby - is_a? v instance_of? - what is the difference?
In Ruby, have you ever wanted to know if an object is of a particular type?
The instance_of? method will return a true value if an object is of the type given as a parameter. However, instance_of? will return a false if we use it to check whether an object inherits from another class. So it's an ...
|Object Orientation in Ruby - intermediate examples
It's when you teach object orientation - the fully Monty, including inheritance, static and dynamic methods and variables, and so on - to a complete novice to programming and that person finds it's one of the easiest parts of the course that you realise:
a) Just how many bad ole habits us ancient structure ...
|Some Ruby programming examples from our course
I was giving a Public Ruby Course to a small group at the end of last week ... and having a small group gave me the opportunity to write some demonstrations in front of them. I have now tidied these up and have pleasure in presenting to more Ruby demonstrations:
Ruby's BEGIN block
The compact method ...
|What are factory and singleton classes?
Do you find some of the OO terminolgy baffling? Once you've learnt about constructors and methods, inheritance, overloading and polymorphism and statics, you might think you're there. Then someone mentions a "factory class" or a "singleton" ...
Fear not - factory and singleton classes are posh names ...
|MTBF of coffee machines
Updated - see end of story
REAL coffee. Mmmmmm. Ever since we started running courses here, we've provided customers with superb coffee. Grind the beans as required, brew the coffee with freshly heated pure water and supply a choice of sugars and sweeteners for those who want such things.
Our first ...
|Think about your design even if you don't use full UML
Even if you don't feel that your project is big enough to get involved with formal design methods, many of the lessons of UML and some informal design diagrams can help you get a clear view of what you're going to be doing be for you start, and can help you come up a good, thought out and reliable plan ...
| Amending a base class even after it has been extended
| Copy v clone
| attribute accessors
| static and dynamic member variables and methods
| Single class example for extension by inheritance
| Object and Class variables (dynamic and static)
| Static Method
| Inheritance in Ruby
| Comparator methods (larger and smaller)
| With $ - variables are global
| no returns on a Ruby method
| default - variables are LOCAL
| Simplest Inheritance Example
| Single class demo
| Inheritance, factories, statics, DATA, lazy ops, to_s
| Inheritance in Ruby
| Sample class used in sharing example
| Class demo - lots of extras
| A class with some class (static) and singleton members
| Operator overloading
| Operator (+) and to_string overloading, Ruby
| Inheritance, Class methods, Attribute Accessors, operator overloading
| second program that uses classes
| Server Log Analysis (2)
| Server Log Analysis
| Listing of standard methods
| gherkin / features of station class
| Cucumber implemented test code for station.feature behaviour
| Sample class used in sharing example
| Data manipulation - object approach
| Data manipulation - structured approach
| third example use of classes
| Class / Interitance / Test program data
| Class / Interitance / Test program example showing good practise
| Cluster of related (inheriting) transport classes and test program
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Public, private and protected visibility.
Singletons and defs.
Inheritance mixins, and super.
Destructors and garbage collection.
Namespaces and modules.
Calling methods with code blocks.
Looking inside objects - reflection.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.