Ruby Dice Roller With Optional Textfile Logging

I did this to see how much Ruby I could remeber. Turns out this is pretty simple.

  1. puts "What sort of dice do you want to roll (I.E. How many sides)?"
  2. sides = gets.chomp.to_i
  3. puts "How many times should the roll be repeated?"
  4. rolls = gets.chomp.to_i
  5. puts "Should the output be logged to a text file? (if so please speicify a filename now, minus the file extension.)"
  6. fname = gets.chomp
  7. if fname == ""
  8.     if sides == 1
  9.     puts "Rolling a die with " + sides.to_s + " sides " + rolls.to_s + " times."
  10.     else
  11.     puts "Rolling dice with " + sides.to_s + " sides " + rolls.to_s + " times."
  12.     end
  13. result = rand(sides) + 1
  14.     while rolls > 0
  15.     result = rand(sides) + 1
  16.     puts result.to_s
  17.     rolls = rolls - 1
  18.     end
  19. else
  20. logf = fname + ".txt"
  21. log = File.open(logf, 'w')
  22.     if sides == 1
  23.     puts "Rolling a die with " + sides.to_s + " sides " + rolls.to_s + " times and logging it to a file called " + fname + ".txt."
  24.     else
  25.     puts "Rolling dice with " + sides.to_s + " sides " + rolls.to_s + " times and logging it to a file called " + fname + ".txt."
  26.     end
  27. log.write("Rolling dice with " + sides.to_s + " sides " + rolls.to_s + " times and logging it to a file called " + fname + ".txt.\n")
  28.     while rolls > 0
  29.     result = rand(sides) + 1
  30.     puts result.to_s
  31.     log.write(result.to_s + ", ")
  32.     rolls = rolls - 1
  33.     end
  34. log.close
  35. end

I forgot, of course, to comment it at all.