Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Ruby module R108
More Classes and Objects
Exercises, examples and other material relating to training module R108. This topic is presented on public courses Learning to program in Ruby, Ruby Programming

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 course module.
Related technical and longer articles
Basic class definition and use in Ruby
Modules, Mixins and Comparators in Ruby

Articles and tips on this subjectupdated
4551Testing 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 ...
4550Build 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 ...
4504Where 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 ...
4366Changing 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   weeklist.extend(weekend) to take a list of (something) for a week and add a list relating to weekend onto the end of it. Sometimes, the method ...
2980Ruby - 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 ...
3782Standard 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):004:1> end => nil irb(main):005:0> widget = Thing.new => #<Thing:0x98c1e74> irb(main):006:0> widget.methods => ...
3781Private, 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 ...
3760Why 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 ...
3260Ruby - 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 ...
3158Ruby 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 ...
3154Changing 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 ...
3142Private 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 ...
2977What 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:   sageroll.push Transact::factory(current) and that call creates an object ... Whilst you can create ...
2717The 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. (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 ...
2620Direct 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. In ...
2616Defining 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 ...
2604Tips 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 ...
2603Ruby 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 ...
2601Ruby - 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 ...
2292Object 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 ...
1587Some 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 ...
1217What 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 ...
184MTBF 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 ...
656Think 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 ...
Examples from our training material
O1.rb   Amending a base class even after it has been extended
O2.rb   Copy v clone
attr   attribute accessors
cmquick   static and dynamic member variables and methods
d4_2   Single class example for extension by inheritance
d4_3   Object and Class variables (dynamic and static)
d4_4   Static Method
d4_5   Inheritance in Ruby
disc2   Comparator methods (larger and smaller)
dub2   With $ - variables are global
dub3   no returns on a Ruby method
dubber   default - variables are LOCAL
finex   Simplest Inheritance Example
food.rb   Single class demo
in4.rb   Inheritance, factories, statics, DATA, lazy ops, to_s
inh1.rb   Inheritance in Ruby
journey.rb   Sample class used in sharing example
multifood.rb   Class demo - lots of extras
ob2.rb   A class with some class (static) and singleton members
olop.rb   Operator overloading
overload_add.rb   Operator (+) and to_string overloading, Ruby
reallyshort   Inheritance, Class methods, Attribute Accessors, operator overloading
second   second program that uses classes
slash   Server Log Analysis (2)
slogan   Server Log Analysis
stanley   Listing of standard methods
station.feature   gherkin / features of station class
station_steps.rb   Cucumber implemented test code for station.feature behaviour
stations.rb   Sample class used in sharing example
stobsort   Data manipulation - object approach
ststsort   Data manipulation - structured approach
third   third example use of classes
transact.data   Class / Interitance / Test program data
transact.rb   Class / Interitance / Test program example showing good practise
transport.rb   Cluster of related (inheriting) transport classes and test program
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from http://www.training-notes.co.uk.
Topics covered in this module
Public, private and protected visibility.
Singletons and defs.
Inheritance mixins, and super.
Destructors and garbage collection.
Namespaces and modules.
Freezing objects.
Calling methods with code blocks.
Looking inside objects - reflection.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses 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. Also available is the Opentalk Forum for discussion of technical questions.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2015: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/R108.html • PAGE BUILT: Sun Oct 11 09:24:39 2015 • BUILD SYSTEM: WomanWithCat