Main Content

Build up classes into applications sharing data types in Ruby

Archive - Originally posted on "The Horse's Mouth" - 2015-10-23 08:51:11 - Graham Ellis

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 stand-alone:

  munchkin:lr grahamellis$ ruby stations.rb
  2445464
  Plymouth
  Distance is 125.04
  Distance is 125.04
  Distance is 125.0
  munchkin:lr grahamellis$


I then went on to add a (second) program that uses that class - see second.

Another class is defined in journey.rb and another program that uses both classes in third ... and so we could go on.

With all this build-up going on, the need for documentation of what's where becomes more important. You'll find the data file for the stations class on our website, but not the journey file as it contains personal information we can't put out in public. But we can show you and explain this program so YOU can be writing programs on our Ruby courses.