Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [Code] Touch Typing Tutor What do you think of my code?

Reply
Thread Tools

[Code] Touch Typing Tutor What do you think of my code?

 
 
Justin brainy
Guest
Posts: n/a
 
      03-09-2011
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
-------------------------------------------------------------------------

Attachments:
http://www.ruby-forum.com/attachment/6015/touchtutor.rb


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
JP Billaud
Guest
Posts: n/a
 
      03-09-2011
Hey,

I think your code looks way too much like C. IMO, if you end up using
C-like loop statement such as while or for it most likely means you are
not doing very good Ruby-like coding.

The way I would do your while loop would be something like this:

=====

#!/usr/bin/ruby

class String
def compare_sum_errors other
cnt = [self.length, other.length].min

(0..(cnt-1)).inject(0) do |errors, idx|
next errors + 1 if self[idx] != other[idx]
errors
end + (self.length - other.length).abs
end
end

str1 = "test origin string"
str2 = "test diff string"

puts str1.compare_sum_errors str2

=====

Hope it helps,

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Adam Prescott
Guest
Posts: n/a
 
      03-09-2011
[Note: parts of this message were removed to make it a legal post.]

On Wed, Mar 9, 2011 at 11:27 PM, JP Billaud <> wrote:

> class String
> def compare_sum_errors other
> cnt = [self.length, other.length].min
>
> (0..(cnt-1)).inject(0) do |errors, idx|
> next errors + 1 if self[idx] != other[idx]
> errors
> end + (self.length - other.length).abs
> end
> end
>
> str1 = "test origin string"
> str2 = "test diff string"
>
> puts str1.compare_sum_errors str2
>


This isn't the best use of inject.

This is much more readable, I find:

def count_errors(first, second)
# reorder things so that first is always <= second
first, second = [first, second].sort_by { |s| s.length }

errors = 0

first.chars.zip(second.chars).each do |f, s|
errors += 1 if f != s
end

# over-typing is an error
errors + (second.length - first.length)
end

count_errors("hello", "helloo") #=> 0
count_errors("hello", "helllo") #=> 2

str1 = "test origin string"
str2 = "test diff string"
count_errors(str1, str2) #=> 13 (same result as the inject)

 
Reply With Quote
 
Justin Wadsworth
Guest
Posts: n/a
 
      03-10-2011
Okay, thanks a lot guys!

I will finish my book and rewrite the program with a more ruby-like
syntax. The thing is I haven't read a lot about methods and classes
yet... guess I'll need some time to get used to Ruby too.

Meanwhile, I will study the code you gave me and figure out your "Ruby
Way".

Again, thank you very much and I hope to help you soon

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Shadowfirebird
Guest
Posts: n/a
 
      03-10-2011
> I will finish my book and rewrite the program with a more ruby-like
> syntax. The thing is I haven't read a lot about methods and classes
> yet... guess I'll need some time to get used to Ruby too.


Methods and classes will allow you to avoid repeating the same code over and over. (For example in your original program you do a lot of print-get-sleep.)

Code that doesn't repeat itself is shorter, easier to test, and easier to understand -- which means that it's easier to fix; much easier.

Ruby programmers talk a lot about DRY: that just means "Don't Repeat Yourself". The golden target for any ruby programmer is never to code the same bit of code more than once, ever. (Of course it never works out quite that way, but it's a nice goal.)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you teach touch typing to a cat? =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 17 08-20-2007 11:58 AM
Typing tutor help script needed, please Throw Python 4 10-28-2005 07:23 PM
Re: Kid's Typing Tutor? bambam Computer Information 3 10-21-2003 08:12 PM
Re: Kid's Typing Tutor? Sirius Computer Information 0 10-14-2003 11:08 PM
Re: Kid's Typing Tutor? pc-mania Computer Information 0 10-14-2003 10:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57