Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
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 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.

O2.rb

clone 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.

dbu

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.

ex

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.

finex

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.

googletrace

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.

ii

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 articles
R050 - Ruby - General
  [4294] A bright new gem - updated Ruby training - (2014-09-16)
  [3799] Ruby Documentation through rdoc - (2012-07-07)
  [2866] Ruby - how does it compare and where is it the right language? - (2010-07-11)
  [2826] Ruby - training for automated testing users - (2010-06-25)
  [2605] Ruby on Rails - a sample application to teach you how - (2010-01-30)
  [2504] Learning to program in ... - (2009-11-15)
  [2227] Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11)
  [2104] Ruby Programming and Rails - 4 different courses in one - (2009-03-26)

R104 - Ruby - Control Structures
  [3769] Muttable v immutable and implications - Ruby - (2012-06-20)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3619] Ruby v Perl - a comparison example - (2012-02-21)
  [3422] Assigning values to variables within other statements - Ruby - (2011-09-07)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3254] Multiple inputs, multiple out, ruby functions - (2011-04-19)
  [3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
  [3156] Splitting data reading code from data processing code - Ruby - (2011-02-04)
  [2975] Why do I need brackets in Ruby ... or Perl, Python, C or Java - (2010-09-29)
  [2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2619] Passing code to procedures and yield in Ruby - (2010-02-02)
  [2471] A short form of if ... then ... else - (2009-10-23)
  [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
  [1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
  [1891] Ruby to access web services - (2008-11-16)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [1870] What to do with a huge crop of apples - (2008-11-04)
  [1738] Clean code, jump free (Example in Lua) - (2008-08-06)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1587] Some Ruby programming examples from our course - (2008-03-21)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [995] Ruby's case - no break - (2006-12-17)
  [985] Equality in Ruby - == eql? and equal? - (2006-12-14)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [960] 1st, 2nd, 3rd revisited in Ruby - (2006-12-02)

R108 - Ruby - More Classes and Objects
  [3782] Standard methods available on all objects in Ruby - (2012-06-23)
  [3781] Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby? - (2012-06-23)
  [3760] Why you should use objects even for short data manipulation programs in Ruby - (2012-06-10)
  [3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
  [3154] Changing a class later on - Ruby - (2011-02-02)
  [3142] Private and Public - and things between - (2011-01-22)
  [2980] Ruby - examples of regular expressions, inheritance and polymorphism - (2010-10-02)
  [2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2623] Object Oriented Ruby - new examples - (2010-02-03)
  [2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
  [2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [2603] Ruby objects - a primer - (2010-01-29)
  [2601] Ruby - is_a? v instance_of? - what is the difference? - (2010-01-27)
  [2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [184] MTBF of coffee machines - (2005-01-20)


Back to
A new monopoly on the ferry to Northern Ireland
Previous and next
or
Horse's mouth home
Forward to
Returning multiple values from a function call in various languages - a comparison
Some other Articles
Keep your business customer-friendly!
TransWilts rail - what picture represents us
Extra courses - Advanced PHP, MySQL and Lua
Ruby training - some fresh examples for string handling applications
A new monopoly on the ferry to Northern Ireland
Rake - a build system using code written in Ruby
Points West to Belfast
4300 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: 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/mouth/3158_Rub ... tions.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat