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.
hiragana.rb
Its purpose is to test your knowledge of two Japanese alphabets, Hiragana and Katakana.
# Japanese Alphabets Quiz, version 1.3 :) 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 # Select a random pair of key and value from the given hash character = alphabet.sort_by{ rand }[0...1] # Turn the result into an array pair = character.shift { |key, value| } # Get the first array value (was the hash key) question = pair.first # Ask the question puts "Which character is " + question + "?" # Gets the answer from the user, strip spaces and turn it lowercase to match the hash case anyway answer = gets.chomp.strip.downcase # Validate user input 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
# In this hash we set the basic hiragana characters and their respective phonetic counterparts $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' }
# In this hash we set the basic katakana characters and their respective phonetic counterparts $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', }