summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Parri <simonparri@ganzeria.com>2023-04-29 22:19:04 -0500
committerSimon Parri <simonparri@ganzeria.com>2023-04-29 22:21:06 -0500
commit1d1358bd923211c457e7015b43c135482c63d3ca (patch)
tree6fac406af4e6e1eaa6c1a68b405fe8cc31408070
parentb99d71047a4fcc7d379bdb0db0169809cf98d343 (diff)
downloadspelling-rbee-1d1358bd923211c457e7015b43c135482c63d3ca.tar.gz
spelling-rbee-1d1358bd923211c457e7015b43c135482c63d3ca.zip
spelling-rbee: Rewrite and reformat some code
+ Remove parentheses around function arguments + Replace $exit with throw/catch + Rewrite ckword and printing code
-rwxr-xr-xspelling-rbee64
1 files changed, 31 insertions, 33 deletions
diff --git a/spelling-rbee b/spelling-rbee
index 60cf6af..58bbcf6 100755
--- a/spelling-rbee
+++ b/spelling-rbee
@@ -3,62 +3,60 @@
require "open-uri"
require "readline"
-def get(prompt="spell: ")
+def get prompt="spell: "
Readline.readline prompt
end
-def say(it,speed=100)
+def say it, speed=100
pid = spawn "espeak -s #{speed} '#{it}'"
Process.detach pid
end
-def ckword(word,result)
- if result == word
- return [word, "correct", result]
- else
- return [word, "wrong", result]
- end
+def ckword orig, got
+ result = orig == got ? :right : :wrong
+ [result, orig, got]
end
def read(file)
open file do |f|
- return f.readlines.map(&:chomp)
+ f.readlines.map(&:chomp)
end
end
-def repl(words)
+def repl words
rr = []
- words.each do |word|
- say word
- r = get
- case r
- when ":r"
- redo
- when ":imacheaterandknowit"
- puts word
- redo
- when ":q"
- $exit = true
+ catch :exit do
+ words.each do |word|
+ say word
+ r = get
+ case r
+ when ":r"
+ redo
+ when ":imacheaterandknowit"
+ puts word
+ redo
+ when ":q"
+ throw :exit
+ end
+ rr.push ckword(word, r)
end
- break if $exit
- rr.push ckword(word, r)
end
- return rr
+ rr
end
-def results(words)
- correct = []
- words.each {|word|
- correct.push(word[0] + ": " + word[1] + ": " + word[2])
- }
- return correct
+def results rr
+ rr.map do |(result, orig, got)|
+ if result == :right
+ "Right: '#{orig}'"
+ else
+ "Wrong: '#{orig}', got '#{got}'"
+ end
+ end
end
-def run(file = (ARGV[0] || "http://zoar.cx/~simon/21k/words.txt"))
- $exit = false
+def run file=ARGV[0]
words = read file
rr = repl words
- return true if $exit
puts results rr
end