Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Ruby off the Rails?

Time was ... "if it's Ruby it must be Rails". The Ruby on Rails framework was the initial killer application that brought the Ruby language to the attention of many, and was so prevelant that it masked other excellent uses. So I'm delighted to see significant other uses of Ruby growing in addition to its use in Rails. Ruby is an excellent, and still underrated, "scripting" language, taking the eclectic best principles from Perl 5 and adding a true underlying Object Oriented philospohy and foundations.

I'm giving a Ruby course this week; I will be running a day to introduce Rails on Friday, but up to that point we're looking at the language - for delegates who are new to programming, rather than for programmers converting from something else. It's our Learning to program in Ruby course.

For readers who may be familiar with programming in other languages, may I present an elegant script which I wrote at the end of yesterday - [here]. I've taken all the records in a data file, each of which contains data about a UK railway station, and created an object from each. I've then enquired of the objects looking for stations where traffic has grown almost five times in six years, and reported on that selected few. That's the overview ... now for the code ...

Open the file of data, and initialiase an "array" to hold each station:

  fh = File.new "railstats.xyz"
  stns = []

Read each station into the array, converting the data on each line from a string of text to a format that's easily useable:

  while (lyne = fh.gets)
    stns.push Station.new(lyne)

Loop through each of the places where there's a station, reporting on the growth if it's between 4.5 and 5 times:

  for place in stns
    growth = place.getgrowth
    if growth > 4.5 and growth < 5.0
      puts "Growth #{growth} at #{place.getname}"

Of course, much of the work that's done here is in the piece of code that defines a Station, how it works and what we can do with it. For the purposes of this demonstration I included the definition in the same program file (it means that the delagates can see it all in one window, and that you can download it all in a single file) ... for a live and developing suite of applications, I would share the station definition between programs by wrining it in a separate file and then calling it up with a require statement.

Anyway - the definintion of a "Station":

  class Station
    def initialize(record_text)
      @fdc, @tlc, @postcode, @gridref,
      @lati, @longi, @name, @t2005,
      @t2006, @t2007, @t2008,
      @t2009, @t2010 = record_text.chomp.split "\t"
    def getgrowth
      return 1.0 if @t2005 == "NULL" or @t2010 == "NULL"
      @t2010.to_f / @t2005.to_f
    def getname
      return @name

There's a great deal more that I could do there, and some design decisions made ... I've elected to name each element from each line and to store them in separate variables not an array, but I've elected to store them all as strings rather than make conversions to other types. I've decided to leave the logic to work out whether or not I can calculate a growth factor until the code is asked for such a factor rather than doing so as soon as each Station is set up, and I've decided that if I can't calculate a growth factor, I'll treat the station as if it had neither growth nor a loss in traffic. The beauty of the structure that Ruby has let me employ is that all these detailed decisions can be left up to the station 'expert' and hidden in this class definition. The person calling the code shown earlier just need to know what questions to ask and how to handle the answers. This 'hidden within' approach is known as encapsulation if you would like a posh word for it.

You can download the data (it's extracted from public data provided by the Office of the Rail Regulator and the Department for Transport amongst others) from [here]. Output looks like this:

  munchkin:rs grahamellis$ ruby thirdrail
  Growth 4.84424038039829 at Bradford Forster Square
  Growth 4.69588971475764 at London Fields
  Growth 4.88378896251115 at Hackney Wick
  Growth 4.62891340332681 at Maghull
  Growth 4.91176855609373 at South Tottenham
  Growth 4.60778231652162 at West Hampstead Thameslink
  Growth 4.72221717401181 at Brondesbury Park
  Growth 4.725 at Havenhouse
  Growth 4.97251091015432 at Ore
  Growth 4.64773309666371 at Worcester Foregate Street
  munchkin:rs grahamellis$

... none of which I have any photos of to illustrate the article ...

My subject line asks "Ruby off the Rails?". Most certainly it is not. It's an excellent language, and this week's a real pleasure letting me introduce programming through Ruby to a group of delegates on our course.

(written 2011-09-06, updated 2013-01-01)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R105 - Ruby - Classes and Objects
  [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
  [2603] Ruby objects - a primer - (2010-01-29)
  [2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
  [2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [4009] Clear, concise examples - Ruby classes and objects. - (2013-02-17)
  [4502] Reading and parsing a JSON object in Ruby - (2015-06-01)

Back to
Making best use of the new enthusiasm for Melksham
Previous and next
Horse's mouth home
Forward to
Assigning values to variables within other statements - Ruby
Some other Articles
Our National Autograss Champion, from Melksham
Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP
1 + 1 + 1 + 1 = 12?
Assigning values to variables within other statements - Ruby
Ruby off the Rails?
Making best use of the new enthusiasm for Melksham
Data that we use during our training courses, and other training resources
Tcl packages, pkg_mkIndex, pkgIndex.tcl -what are they and why use them.
What is a namespace and why do we need them?
Storing Tcl source code encoded, and running via your own C program
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2023: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3421_Rub ... ails-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb