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 text handling, so this week I found myself writing a number of new, tuned examples during the private Ruby Course
that I was running. I'm putting many of them onto our web site - the first batch is below - so that my delegates can get back to them, and so that they form a resource for others too.
is one of the standard methods that's inherited from Ruby's basic object, so is available on any object you create. If you simply copy an object through assignment, you're just duplicating its instance variable - i.e. providing another name for the same data, but if you clone it you actually duplicate the contents.
What's the significance? If you simply assign, it's much more efficient ... and any changes you then make are made to both names, which is how you can pass objects into methods and have them modified within. But if you clone, you end up with a distinct additional object that can be modified and manipulated separately; that's a much less common requirement, but it is
needed from time to time.
In Ruby, you can run a function or method by just using its name - there is no need to put a pair of brackets after it as there is in Python to say "run this code". This is good news until you have a variable and a function of the same name, and then the question comes "how does Ruby tell them apart?
The answer is that - where a name has been used for both a variable and a function - that the variable i used if no brackets are given after the name, but the function is run if brackets are used - in other words, it reverts to the Python behavior.
Note also in this example that we can redefine a function in Ruby - the second one simply replaces the first. This is an enabling feature behind mixins, and allows us to come back and modify classes at a later point too.
Ruby, like Perl, has a baffling array of conditional and loop statements and on our courses I like to set one
practical which I ask delegates to complete without using the word "if" or the word "while".
This week, I set a question to ask the delegates to read in the number of course dinners we need to order each day ... with a test to see if the number is in a valid range, a test to see if a special code has been entered to indicate the course has been completed, and a different action to be taken if the number entered is zero.
The simplest possible example of inheritance in Ruby? Almost ... we've defined an animal base class with just a constructor, and then added in two derived classes - Pet and Farm, each with its own constructor. Those have each called the base class constructor so that the common setup code (for name and weight) does not need to be repeated.
Rather than provide separate methods to read values from our objects, we have declared a attr_reader
which allows the variables within the object to be accessed directly. Although such direct variable access is commonly regarded as poor practice in other languages (as it locks the application code to the variable), it is perfectly acceptable in Ruby, where a variable name and a method name are interchangeable, and the class can be later updated if necessary to add logic.
It's surprising how just a few lines of Ruby can make for quite a powerful application. Google's crawler - the Googlebot - visits our web site regularly and indexes our pages, and I wanted to go through our web logs for yesterday and see where it had been, which pages it had checked several times, etc.
This short program also provides a good demonstration of putting many Ruby features together - you'll find string and regular expression matches,arrays and hashes, sorting and various types of looping, and file handling too, all within a simple page of code.
A further Ruby course exercise. Scenario: You have 17 nieces and nephews, and having just won the lottery you are going to distribute $1000.00 between them for Christmas ... or are you?
Little Billy glued your slippers to the floor while you were sleeping in front of the fire one evening, and when you got up, you tripped and stubbed your nose on the fire place. He gets nothing ...
And Jessica, who used to be your favorite, balanced a bucket of water on top of the door and it fell on you and drenched you when you pushed the door open - she gets nothing either
Then there's Jane ...
Create a table showing how many remain in favor, and how much money each will get if you split the money between just those few. (written 2011-02-05, updated 2011-02-10)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesR050 - Ruby - General 
Back in the saddle again - excellent open source course from Well House Consultants - (2015-11-26) 
A bright new gem - updated Ruby training - (2014-09-16) 
Ruby Documentation through rdoc - (2012-07-07) 
Ruby - how does it compare and where is it the right language? - (2010-07-11) 
Ruby - training for automated testing users - (2010-06-25) 
Ruby on Rails - a sample application to teach you how - (2010-01-30) 
Learning to program in ... - (2009-11-15) 
Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11) 
Ruby Programming and Rails - 4 different courses in one - (2009-03-26)R104 - Ruby - Control Structures 
Alternating valuses / flip-flop / toggle - example in Ruby - (2016-05-17) 
Where does Ruby load modules from, and how to load from current directory - (2015-06-03) 
Separating your code for easier testing, understanding and re-use; example in Ruby - (2015-06-02) 
Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19) 
Conditionals, loops and methods in Ruby - a primer with simple examples - (2014-12-29) 
Learning to program - Loop statements such as while - (2014-11-22) 
Learning to Program - the conditional statement (if) - (2014-11-21) 
Muttable v immutable and implications - Ruby - (2012-06-20) 
Finding the total, average, minimum and maximum in a program - (2012-02-22) 
Ruby v Perl - a comparison example - (2012-02-21) 
Assigning values to variables within other statements - Ruby - (2011-09-07) 
Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18) 
Multiple inputs, multiple out, ruby functions - (2011-04-19) 
Is this number between? Does this list include? - Ruby - (2011-04-18) 
How a for loop works Java, Perl and other languages - (2011-03-12) 
Returning multiple values from a function call in various languages - a comparison - (2011-02-06) 
Splitting data reading code from data processing code - Ruby - (2011-02-04) 
Why do I need brackets in Ruby ... or Perl, Python, C or Java - (2010-09-29) 
Alternative loops and conditionals in Ruby and Perl - (2010-07-28) 
For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07) 
Passing code to procedures and yield in Ruby - (2010-02-02) 
A short form of if ... then ... else - (2009-10-23) 
Learning to program in Ruby - examples of the programming basics - (2009-07-15) 
Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23) 
Ruby to access web services - (2008-11-16) 
Ruby Programming Course - Saturday and Sunday - (2008-11-16) 
What to do with a huge crop of apples - (2008-11-04) 
Clean code, jump free (Example in Lua) - (2008-08-06) 
Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04) 
Some Ruby programming examples from our course - (2008-03-21) 
Ruby, C, Java and more - getting out of loops - (2008-03-19) 
for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06) 
A better alternative to cutting and pasting code - (2007-04-26) 
Ruby's case - no break - (2006-12-17) 
Equality in Ruby - == eql? and equal? - (2006-12-14) 
Breaking a loop - Ruby and other languages - (2006-12-03) 
1st, 2nd, 3rd revisited in Ruby - (2006-12-02)R108 - Ruby - More Classes and Objects 
Testing your new class - first steps with cucumber - (2015-10-23) 
Build up classes into applications sharing data types in Ruby - (2015-10-23) 
Changing what operators do on objects - a comparison across different programming languages - (2014-12-26) 
Standard methods available on all objects in Ruby - (2012-06-23) 
Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby? - (2012-06-23) 
Why you should use objects even for short data manipulation programs in Ruby - (2012-06-10) 
Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23) 
Changing a class later on - Ruby - (2011-02-02) 
Private and Public - and things between - (2011-01-22) 
Ruby - examples of regular expressions, inheritance and polymorphism - (2010-10-02) 
What is a factory method and why use one? - Example in Ruby - (2010-09-30) 
The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11) 
Object Oriented Ruby - new examples - (2010-02-03) 
Direct access to object variable (attributes) in Ruby - (2010-02-02) 
Defining a static method - Java, Python and Ruby - (2010-02-01) 
Tips for writing a test program (Ruby / Python / Java) - (2010-01-29) 
Ruby objects - a primer - (2010-01-29) 
Ruby - is_a? v instance_of? - what is the difference? - (2010-01-27) 
Object Orientation in Ruby - intermediate examples - (2009-07-16) 
What are factory and singleton classes? - (2007-06-04) 
Think about your design even if you don't use full UML - (2006-03-24) 
MTBF of coffee machines - (2005-01-20)
Some other Articles
Keep your business customer-friendly!TransWilts rail - what picture represents usExtra courses - Advanced PHP, MySQL and LuaRuby training - some fresh examples for string handling applicationsA new monopoly on the ferry to Northern IrelandRake - a build system using code written in RubyPoints West to Belfast