From: Justin brainy Date: 2011-03-10T04:25:20+09:00 Subject: [Code] Touch Typing Tutor What do you think of my code? Hello! I'm new to Ruby, but I spent some time with C. I would like to know if the program I wrote was syntaxically correct for a Ruby program. For example, I used a while loop to iterate through a string (with a "i") to compare two strings... Should I have used Ruby's iterator? Basically, the program is a *very* simple touch typing tutor. It reads through a file to set the "lecture, for example : "jfjf fff ff jjf fj jjjf fj" and then prints the line and asks the user for his answer. It calculates number of errors (n_erreurs in french), precision in %, time (okay too?). Thanks ! ------------------------------------------------------------------------ [code] #!/usr/bin/ruby # Fichier de lecture f = File.open("lecture.txt" ,"r") s_file = f.readline f.close # Variables & Constantes n_erreurs = precision = 0 n_char = s_file.length BUT = 95 # % L_PAGE = 40 puts "Tapez le texte suivant :" puts print "Debute dans 3 ." sleep 1 print " 2 ." sleep 1 puts "1" sleep 1 puts puts s_file t_1 = Time.now s_input = gets.chomp t_2 = Time.now i = 0 puts puts "Resultats".center(L_PAGE) while(i.to_i < n_char) # Comparaison des deux chaines if (s_file[i] != s_input[i]) n_erreurs = n_erreurs + 1 # puts "Erreur : ##{i}" end i = i + 1 end precision = (100 - ( n_erreurs.to_f / n_char ) * 100).floor puts puts "Nombre d'erreurs : #{n_erreurs}" puts "Nombre de caracteres : #{n_char}" puts "Precision : #{precision} %" puts "Temps : #{(t_2 - t_1).floor} s" puts (precision > BUT) ? "Objectif atteint !" : "Objectif manque" puts [/code] ------------------------------------------------------------------------- Attachments: http://www.ruby-forum.com/attachment/6015/touchtutor.rb -- Posted via http://www.ruby-forum.com/.