# Outputting results as html in a file

**URL:** <https://www.ruby-forum.com/t/outputting-results-as-html-in-a-file/116087>\
**Category:** Ruby\
**Created:** [October 2, 2007, 7:20am UTC](https://www.ruby-forum.com/t/outputting-results-as-html-in-a-file/116087 "2007-10-02T07:20:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jenny\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19adc/32.png) [@Jenny\_P](https://www.ruby-forum.com/u/Jenny_P)\
**Post date:** [October 2, 2007, 7:20am UTC](https://www.ruby-forum.com/t/outputting-results-as-html-in-a-file/116087/1 "2007-10-02T07:20:36Z")

</div>

Background: I’m working on a Ruby program to simulate horse races for  
a fantasy horse racing league. It works off a csv file I put together  
by hand with the information necessary for each race.

I realize it would be easier for people in the group to follow if I  
was programming with objects. However, I just don’t understand OO  
programming, my skills (such as they are) are procedural. Ruby was  
pointed out to me as a language that would allow me to write  
procedural programs and so far it is working out very nicely in that  
regard.

After much hammering and fiddling, I have expanded the program  
significantly since I last posted to the group. I can now run multiple  
races off a single csv file, and I’ve added some additional features I  
wanted.

What I’d like to be able to do, now, is provide minimally formatted  
html “results” for each race.

What I’d like is output that would be something like this:

1a Mdn 2yoC/G Trot $3500   
  
Winner: Ima Winnah, 2005 chestnut colt ABC  
  
Bred by: Suzie Creamcheese  
  
T= 2:00.1f  
  
 1(8) Ima Winnah, 05 cc, ABC, unplcd, pp4, by  
neck, Given pace and in control from gate to wire, T=2:00.1f

Using my variable names, it would be something like this:

race\_name   
 (race #1)  
Winner, details, time, etc, not coded into original program yet   
  
  
 position[j](count + 1) horse\_name[j], horse\_info[j],  
owners\_initials[j], best\_result[j], pp pole\_new[j], by \<margin - not  
coded yet\>, \<Space for comments, added by hand for each race\>, T=  
  
  
\<details on horse #2\>  
  
\<details on horse #3\>

race\_name  
 (race #2)

For the most part, once someone shows me the basics of doing this, I  
can fill in the rest.

Code included below:

#! /usr/bin/env ruby

require ‘csv’

# Written by Jenny P. and Anthony Purcell based off starter code

# provided by Robbert Haarman

# assign counter to keep track of number of horses

count=-1

# create new arrays

horse\_name=Array.new(20)  
horse\_info=Array.new(20)  
owners\_initials=Array.new(20)  
best\_result=Array.new(20)  
race\_style=Array.new(20)  
best\_time=Array.new(20)  
pre\_points=Array.new(20)  
start\_points=Array.new(20)  
luck\_roll=Array.new(20)  
roll\_1=Array.new(20)  
roll\_2=Array.new(20)  
roll\_3=Array.new(20)  
roll\_4=Array.new(20)  
roll\_5=Array.new(20)  
score=Array.new(20)  
pre\_point\_string = String.new  
race\_name = String.new  
race = 0  
file\_lines = Array.new

# For each record in the CSV file…

CSV::Reader.parse(File.open(‘horses2.csv’)) do |record|

test\_string = record[0]  
if test\_string == “” || test\_string == nil then  
if race == 1 then  
# Randomizing Post Position  
# i is a counter to count up to the number of horses  
pole = Array.new(count + 1) { |m| m + 1 }  
number = Array.new(count + 1) { |m| m + 1 }  
position = Array.new(count + 1)  
pole\_new=pole.sort\_by { rand }

```
  # Determining each horse's finishing position
  temp_score = Array.new(count + 1) {|c| 100 * score[c] + 20 -

```

pole\_new[c]}  
sort\_score = temp\_score.sort  
sort\_score.reverse!  
number.each {|a| position[a - 1] = number.detect {|b|  
sort\_score[b - 1] == temp\_score[a - 1]}}

```
  i=0
  line = CSV.generate_line([race_name])
  file_lines.push(line)
  while i <= count do
    j = number.detect {|c| position[c - 1] == i + 1}
    j = j - 1
    # Push each horse's name, owner, score, pre_points, dice

```

rolls, and other info into a new csv file  
line = CSV.generate\_line([“pp#{pole\_new[j]}”, horse\_name[j],  
horse\_info[j], owners\_initials[j], best\_result[j], race\_style[j],  
best\_time[j], “#{pre\_points[j]} (#{start\_points[j]} #{luck\_roll[j]})”  
, roll\_1[j], roll\_2[j], roll\_3[j], roll\_4[j], roll\_5[j], score[j],  
“#{position[j]}(#{count + 1})”])

```
    # Here is where we put the new line on the end of the csv

```

array  
file\_lines.push(line)  
i = i + 1  
end  
# generate a blank line and write it  
line = CSV.generate\_line([])  
file\_lines.push(line)

```
end
race = 0

```

elsif race == 0 then  
race = 1  
count = -1  
race\_name = record[0]  
else  
count = count + 1 # count the number of horses in a particular  
race  
# Now we assign the appropriate fields of the record to  
# variables named after the fields in the CSV file.  
horse\_name[count] = record[0]  
horse\_info[count] = record[1]  
owners\_initials[count] = record[2]  
best\_result[count] = record[3]  
race\_style[count] = record[4]  
best\_time[count] = record[5]  
pre\_point\_string = record[6]

```
# Extract lead character for luck rolls
pre_roll_check = pre_point_string[0, 1]
temp_string = pre_point_string[1, pre_point_string.length]

# Use the lead character to determine how many luck rolls are

```

needed  
# Use the remainder of the input string to evaluate initial point  
allotment  
if pre\_roll\_check == “a” then  
num\_rolls = 3  
elsif pre\_roll\_check == “b” then  
num\_rolls = 2  
elsif pre\_roll\_check == “c” then  
num\_rolls = 1  
else  
num\_rolls = 0  
luck\_roll[count] = 0  
pre\_points[count] = pre\_point\_string.to\_i  
start\_points[count] = pre\_points[count]  
end

```
# Generate luck rolls if needed to calculate pre-point total
if num_rolls > 0 then
  pre_roll_dice = (1..num_rolls).map { rand(4) + 1}
  pre_points[count] = temp_string.to_i
  start_points[count] = pre_points[count]
  luck_roll[count] = 0
  pre_roll_dice.each { |p| pre_points[count] = pre_points[count] +

```

p }  
pre\_roll\_dice.each { |q| luck\_roll[count] = luck\_roll[count] + q  
}  
end

```
#puts CSV.generate_line([num_rolls, pre_points[count],

```

temp\_string, pre\_roll\_check])

```
# Roll the dice.
# This generates an array containing five die rolls,
# each die roll ranging from 1 to 6
dice = (1..5).map { rand(6) + 1 }

# Calculate score.
# The score is the sum pre_points, and each of the die rolls
temp_score = pre_points[count]
dice.each { |n| temp_score = temp_score + n }
score[count]=temp_score
roll_1[count]=dice[0]
roll_2[count]=dice[1]
roll_3[count]=dice[2]
roll_4[count]=dice[3]
roll_5[count]=dice[4]

# Share out pre-points based on running style

# Assign margins based on final points

# end of over-arching conditional statement

```

end

```
#end of the "do" statement for reading in CSV files

```

end

```
# Write to a csv file to archive "full" results (all points, etc).
filename = 'results.csv'
File.open filename, 'w+' do |f|
f.puts file_lines
end
```

---

<div class="post-metadata">

**Author:** ![Jenny\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19adc/32.png) [@Jenny\_P](https://www.ruby-forum.com/u/Jenny_P)\
**Post date:** [October 2, 2007, 9:50am UTC](https://www.ruby-forum.com/t/outputting-results-as-html-in-a-file/116087/2 "2007-10-02T09:50:59Z")

</div>

On 10/2/07, Jenny P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Background: I’m working on a Ruby program to simulate horse races for  
> a fantasy horse racing league. It works off a csv file I put together  
> by hand with the information necessary for each race.

> What I’d like to be able to do, now, is provide minimally formatted  
> html “results” for each race.

Hi,

I think you should take a look into ERB  
([http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/)). With this you can  
have a template file or string with the HTML and a syntax to evaluate  
some ruby code inside it, like evaluating your variables, etc. You can  
then output the result of evaluating the template back into a file.

Hope this helps,

Jesus.

PS: sorry, I don’t have time now to work out an example, but I hope  
this gets you started.
