Ruby is an excellent place to get started with OO as it requires *much* less repetitious coding than, say, Java.
I've taken the liberty of changing your code slightly to use a Horse class and keep your horses in a collection 'horses'. I've tried to stay as true to the original code as possible, just changing what's required.
Basically, I've removed all the arrays into a Horse class which we can use to create new horses by calling its 'new' method with the line from the csv file (Horse.new calls Horse::initialize. I have not idea why the different names, just something to be aware know)
Cheers,
Dave
#! /usr/bin/env ruby
require 'csv'
# Written by Jenny Purcell based off starter code
# provided by Robbert Haarman
class Horse
attr_reader :horse_name, :horse_info, :owners_initials, :best_result,
:race_style, :best_time, :pre_points, :rolls, :score
attr_accessor :new_pole
def initialize(record)
# Now we assign the appropriate fields of the record to
# variables named after the fields in the CSV file.
# Note that we need to call .to_i on the 6th field
# to convert it from strings to integers.
@horse_name = record[0]
@horse_info = record[1]
@owners_initials = record[2]
@best_result = record[3]
@race_style = record[4]
@best_time = record[5]
@pre_points = record[6].to_i
# Roll the dice.
# This generates an array containing five die rolls,
# each die roll ranging from 1 to 6
@rolls= (1..5).map { rand(6) + 1 }
# Calculate score.
# The score is the sum pre_points, and each of the die rolls
@score= @rolls.inject(pre_points){|sum, i| sum+ i}
end # initialize
end # Horse
# create an array to keep all our horses in
horses=
# For each record in the CSV file...
CSV::Reader.parse(File.open('horses.csv')) do |record|
horses << Horse.new(record)
end # record
# Randomizing Post Position
Array.new(horses.size){ |m| m + 1 }.sort_by{ rand }.each_with_index do |pole, index|
horses[index].new_pole= pole
end # pole, index
horses.each do |horse|
puts CSV.generate_line([
horse.pole_new,
horse.horse_name,
horse.horse_info,
horse.owners_initials,
horse.best_result,
horse.race_style,
horse.best_time,
horse.pre_points]+
horse.rolls+
[horse.score])
i = i + 1
end # horse
# Features to add
# Break ties.
# Have it calculate race times
# Have it calculate margins
# Have it share out pre_points based on running style (adapt to
# numbered "legs")
# Have it export to basic HTML file (for archive/email results)
# all sixes = possible NTR?
# Two consecutive ones is a break in pace.
···
On 22/07/2007, at 1:59 PM, Jenny Purcell wrote:
However, I just don't understand OO
programming, my skills (such as they are) are procedural.