| |||||||||||
| |||||||||||
All together - class, factory, comparable, exception, files, strings, arrays
this example from a Well House Consultants training course
Source code: rs06 Module: R050
# Sample line of data from file:
# 3230 BPW BS34 8PU ST 623796 51.5138357449 -2.5426595271 # Bristol Parkway 1456350 1633280 1789848 1897941 2084200 2041548 # The real power of a language can be seen when all the parts of it come together - which # is what we've done in this example. You should find a link from the web page on which this # sample is posted to explain many of the element that are used class Station include Comparable 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 def get(name) return @info[name] end def set(name,val) return (@info[name] = val) end 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 def to_s return @info["name"] + "[" + @info["tlc"] + "]" end def + (second) obdata = Hash.new @info.keys.each do |element| p element obdata[element] = @info[element] + second.get(element) end # etc .... for the expert on stations to work out how codes would go result = second.clone result.set("name","Grand Central") return result end def <=> (second) diff = @info["2010"].to_i - second.get("2010").to_i return diff end end # -------------------------------------------------------- begin fho = File.new("railstats.xyz","r") rescue StandardError STDERR.puts "oops!" else table = Hash.new while lyne = fho.gets if gotten = Station.factory(lyne) code = gotten.get("tlc") table[code] = gotten end end table.keys.each do |tlc| if table[tlc].get("name") =~ /ington/ puts "#{table[tlc]} saw #{table[tlc].get("2010")} passengers" end end sbf = table["SBF"] sbv = table["SBV"] sbc = sbf + sbv puts sbc if sbf < sbv puts "#{sbf} is the quieter" else puts "#{sbv} is the quieter" end ensure puts "Shoelaces tied" end __END__ munchkin:chruby grahamellis$ ruby rs06 Cramlington[CRM] saw 87374 passengers Cheddington[CED] saw 69732 passengers Workington[WKG] saw 165218 passengers Pleasington[PLS] saw 8878 passengers Adlington (Cheshire)[ADC] saw 13618 passengers Erdington[ERD] saw 575628 passengers Adlington (Lancashire)[ADL] saw 94610 passengers Brinnington[BNT] saw 70816 passengers Durrington-On-Sea[DUR] saw 656790 passengers Widdrington[WDD] saw 6398 passengers Darlington[DAR] saw 2164428 passengers Acklington[ACK] saw 268 passengers Accrington[ACR] saw 279442 passengers Teddington[TED] saw 2222352 passengers Lidlington[LID] saw 21028 passengers Ruskington[RKT] saw 80838 passengers Willington[WIL] saw 10318 passengers Hayes & Harlington[HAY] saw 1581238 passengers Warblington[WBL] saw 32690 passengers Warrington Bank Quay[WBQ] saw 1073842 passengers Bebington[BEB] saw 1136626 passengers Cuddington[CUD] saw 53368 passengers Warrington Central[WAC] saw 1007372 passengers Wennington[WNN] saw 3222 passengers Orpington[ORP] saw 4867836 passengers Watlington[WTG] saw 107956 passengers Aldrington[AGT] saw 157872 passengers Bridlington[BDT] saw 401594 passengers Hillington East[HLE] saw 201690 passengers Kensington Olympia[KPA] saw 1833537 passengers Harlington[HLN] saw 290378 passengers Hillington West[HLW] saw 232424 passengers Leamington Spa[LMS] saw 1772398 passengers Paddington[PAD] saw 29104198 passengers Heckington[HEC] saw 55022 passengers Newington[NGT] saw 139902 passengers Harrington[HRR] saw 20518 passengers Heighington[HEI] saw 7538 passengers Birchington-On-Sea[BCH] saw 273564 passengers Eastrington[EGN] saw 1380 passengers Cressington[CSG] saw 424174 passengers Lymington Pier[LYP] saw 160450 passengers Chessington North[CSN] saw 492516 passengers Lymington Town[LYT] saw 202518 passengers Chessington South[CSS] saw 505480 passengers Stoke Newington[SKW] saw 439528 passengers Wellington[WLN] saw 533368 passengers Wallington[WLT] saw 1772890 passengers Collington[CLL] saw 132246 passengers "name" "tlc" "postcode" "2005" "2006" "2007" "2008" "2010" "2009" "four" "lat" "long" "grid" Grand Central[SBV] St.Budeaux Ferry Road[SBF] is the quieter Shoelaces tied Learn about this subject
This module and example are covered as required on private courses.
Should you wish to cover this example and associated subjects, and you're attending a public course
to cover other topics with us, please see our extra topic program.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Ruby are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "this" training module. You'll find a description of the topic and some
other closely related examples on the "this" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |