This is my first Ruby script. It requires two files, hiragana.rb and katakana.rb (I'm pasting them here as well).
Its purpose is to test your knowledge of two Japanese alphabets, Hiragana and Katakana.
puts "Would you rather test your hiragana (1) or katakana (2) knowledge?"
choose = gets.chomp.to_i
if
choose == 1
puts "OK, hiragana, then!"
require 'hiragana.rb'
lang_name = "hiragana"
else
puts "OK, katakana, then!"
require 'katakana.rb'
lang_name = "katakana"
end
$score = 0
10.times do
alphabet = $characters
character = alphabet.sort_by{ rand }[0...1]
pair = character.shift { |key, value| }
question = pair.first
puts "Which character is " + question + "?"
answer = gets.chomp.strip.downcase
if
answer == pair.last
$score += 1
puts "Correct! " + question + " is " + pair.last + "."
else
puts "Wrong! " + question + " is " + pair.last + "."
end
end
def grade (points)
case points
when (0..3)
"Bad"
when (4..6)
"Average"
when (7..9)
"Excellent"
when (10)
"Perfect"
end
end
puts "End of " + lang_name + " quiz. You scored " + $score.to_s + " out of 10."
puts "Your grade is " + grade( $score ) + "."
hiragana.rb
$characters =
{
'あ' => 'a',
'い' => 'i',
'う' => 'u',
'え' => 'e',
'お' => 'o',
'か' => 'ka',
'き' => 'ki',
'く' => 'ku',
'け' => 'ke',
'こ' => 'ko',
'さ' => 'sa',
'し' => 'si',
'す' => 'su',
'せ' => 'se',
'そ' => 'so',
'た' => 'ta',
'ち' => 'ti',
'つ' => 'tu',
'て' => 'te',
'と' => 'to',
'な' => 'na',
'に' => 'ni',
'ぬ' => 'nu',
'ね' => 'ne',
'の' => 'no',
'は' => 'ha',
'ひ' => 'hi',
'ふ' => 'hu',
'へ' => 'he',
'ほ' => 'ho',
'ま' => 'ma',
'み' => 'mi',
'む' => 'mu',
'め' => 'me',
'も' => 'mo',
'や' => 'ya',
'ゆ' => 'yu',
'よ' => 'yo',
'ら' => 'ra',
'り' => 'ri',
'る' => 'ru',
'れ' => 're',
'ろ' => 'ro',
'わ' => 'wa',
'を' => 'wo',
'ん' => 'n'
}
$characters =
{
'ア' => 'a',
'イ' => 'i',
'ウ' => 'u',
'エ' => 'e',
'オ' => 'o',
'カ' => 'ka',
'キ' => 'ki',
'ク' => 'ku',
'ケ' => 'ke',
'コ' => 'ko',
'サ' => 'sa',
'シ' => 'si',
'ス' => 'su',
'セ' => 'se',
'ソ' => 'so',
'タ' => 'ta',
'チ' => 'ti',
'ツ' => 'tu',
'テ' => 'te',
'ト' => 'to',
'ナ' => 'na',
'ニ' => 'ni',
'ヌ' => 'nu',
'ネ' => 'ne',
'ノ' => 'no',
'ハ' => 'ha',
'ヒ' => 'hi',
'フ' => 'hu',
'ヘ' => 'he',
'ホ' => 'ho',
'マ' => 'ma',
'ミ' => 'mi',
'ム' => 'mu',
'メ' => 'me',
'モ' => 'mo',
'ヤ' => 'ya',
'イ' => 'yi',
'ユ' => 'yu',
'ヨ' => 'yo',
'ラ' => 'ra',
'リ' => 'ri',
'ル' => 'ru',
'レ' => 're',
'ロ' => 'ro',
'ワ' => 'wa',
'ウ' => 'wy',
'ヲ' => 'wo',
}