Ruby - a training example that puts many language elements together to demonstrate the whole
Archive - Originally posted on "The Horse's Mouth" - 2011-04-23 11:52:44 - Graham EllisTowards 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 code ...
1. We've set up objects with quite a number of properties, using an array of elements passed in to the constructor. The data values themselves are then stored in a hash within the object. It saves a lot of repeating code, and it allows us to provide generic "get" and "set" methods. Here's the constructor:
def initialize(parts)
fn = %w(four tlc postcode grid lat long name 2005 2006 2007 2008 2009 2010)
@info = Hash.new
for num in 0...fn.length
@info[fn[num]] = parts[num]
end
end
2. The incoming data for our application comes from a tab delimited file - and the string that we read from the file is not the sort of raw data we want to pass into the constructor. That's because other applications using the same class will have the data supplied in other formats. So we have used a factory method in the class to covert the string into an object:
def Station.factory(raw)
parts = raw.chomp.split("\t")
if parts[7].to_i > 0 && parts[12].to_i > 0
result = Station.new(parts)
else
result = nil
end
return result
end
You'll note that our factory is a static method, and that it does more that just return an object of the desired type; it can also return a false value (nil) if the data passed in doesn't describe an object, and indeed it could be extended to create all sorts of different object types based in the incoming data. The call to the factory is as follows:
if gotten = Station.factory(lyne)
which takes a line of text (in "lyne") and sets up an object which it saves into "gotten" ... which is immediately checked; the handling code for the object is in the following conditional block.
3. What happens when you compare two objects? It's going to fairly obvious for numbers, but when you com eto more complex objects - railway stations in our case - it's not so obvious. In Ruby we could redefine what the operators <, <=, ==, !=, > and >= do (as they're just methods called up with a different syntax), but in practice we'll do better to call in the comparable mixin, and which defines each of those six in terms of a seventh - <=> - and then we can redefine just that one method.
Here's how we load the six-to-one mapping:
include Comparable
and then here's how we use the mapping:
def <=> (second)
diff = @info["2010"].to_i - second.get("2010").to_i
return diff
end
Which will return negative (less than), 0 (equal) and positive (greater than)
4. What do you get when you print out an object? The default's going to be a string that includes the object type and the address in memory it's held at. Maybe that's fine for debugging purposes, but it's a frightener to the enduser of your program. So you can redefine what's produced by printing an object by overloading the to_s method. (If you want to dump out everything in the object, take a look at the inspect method).
def to_s
return @info["name"] + "[" + @info["tlc"] + "]"
end
In our example, we've also overwritten the "+" method so that we can add two stations together. The algorithm is just a demonstration in this case - there would need to be more complex logic to combine each attribute in a suitable way, but the purpose of the course was to teach programming rather that the detail of rail management!
5. If something in your application fails, you can catch the error by including the code that may not work into a begin / end block, with a rescue block telling the program what to do in the event of problems:
  begin
    fho = File.new("railstats.xyz","r")
  rescue StandardError
    STDERR.puts "oops!"
  else
    # [[ code to run if the file open worked ]]
  end
You'll note that I've written the error message to STDERR - that's so that any output redirection will not be applied to this particular message and it will usually appear on the user's screen rather than ending up in the output file.