class Hiragana

Public Class Methods

new() click to toggle source
# File lib/hiragana.rb, line 6
def initialize
  @alphabet = {'a'=>'あ', 'i'=>'い', 'u'=>'う', 'e'=>'え', 'o'=>'お',
               'ka'=>'か', 'ki'=>'き', 'ku'=>'く', 'ke'=>'け', 'ko'=>'こ',
               'sa'=>'さ', 'shi'=>'し', 'su'=>'す', 'se'=>'せ', 'so'=>'そ',
               'ta'=>'た', 'chi'=>'ち', 'tsu'=>'つ','te'=>'て', 'to'=>'と',
               'na'=>'な', 'ni'=>'に', 'nu'=>'ぬ', 'ne'=>'ね', 'no'=>'の',
               'ha'=>'は', 'hi'=>'ひ', 'fu'=>'ふ', 'he'=>'へ', 'ho'=>'ほ',
               'ma'=>'ま', 'mi'=>'み', 'mu'=>'む', 'me'=>'め', 'mo'=>'も',
               'ya'=>'や', 'yu'=>'ゆ', 'yo'=>'よ',
               'ra'=>'ら', 'ri'=>'り', 'ru'=>'る', 're'=>'れ', 'ro'=>'ろ',
               'wa'=>'わ', 'wo'=>'を',
               'n'=>"ん",

               'ga'=>'が', 'gi'=>'ぎ', 'gu'=>'ぐ', 'ge'=>'げ', 'go'=>'ご',
               'za'=>'ざ', 'ji'=>'じ', 'zu'=>'ず', 'ze'=>'ぜ', 'zo'=>'ぞ',
               'da'=>'だ', 'dji'=>'ぢ', 'dzu'=>'づ', 'de'=>'で', 'do'=>'ど',
               'ba'=>'ば', 'bi'=>'び', 'bu'=>'ぶ', 'be'=>'べ', 'bo'=>'ぼ',
               'pa'=>'ぱ', 'pi'=>'ぴ', 'pu'=>'ぷ', 'pe'=>'ぺ', 'po'=>'ぽ',

               'kya'=>'きゃ', 'kyu'=>'きゅ', 'kyo'=>'きょ',
               'gya'=>'ぎゃ', 'gyu'=>'ぎゅ', 'gyo'=>'ぎょ',
               'sha'=>'しゃ', 'shu'=>'しゅ', 'sho'=>'しょ',
               'ja'=>'じゃ', 'ju'=>'じゅ', 'jo'=>'じょ',
               'cha'=>'ちゃ', 'chu'=>'ちゅ', 'cho'=>'ちょ',
               'nya'=>'にゃ', 'nyu'=>'にゅ', 'nyo'=>'にょ',
               'hya'=>'ひゃ', 'hyu'=>'ひゅ', 'hyo'=>'ひょ',
               'bya'=>'びゃ', 'byu'=>'びゅ', 'byo'=>'びょ',
               'pya'=>'ぴゃ', 'pyu'=>'ぴゅ', 'pyo'=>'ぴょ',
               'mya'=>'みゃ', 'myu'=>'みゅ', 'myo'=>'みょ',
               'rya'=>'りゃ', 'ryu'=>'りゅ', 'ryo'=>'りょ'}


  puts 'Romaji to Hiragana'
  puts '(clear) Clear (save) Save (quit) Main menu (alphabet) hiragana alphabet'
  puts '(Type one letter by time ex: o+ENTER, ha+ENTER, yo+ENTER, u+ENTER => おはよう)'

  word = [] # array to store the letters typed

  letter=ask()
  while letter != 'quit' do
    word << @alphabet[letter] # appendind letters into the word array
    
    if letter == 'clear' then
      word.clear()
      puts 'Your word is empty!'
    elsif letter == ' ' then
      puts 'Space inserted. Start a new word!'
      word << letter
      word.each do |w| print "#{w}" end # iterating values
    elsif letter == 'save'
      print 'Enter the file name: '
      filename = gets.chomp.downcase
      s = Save.new(filename, word.join) # join method put all the letters together into a single string
      puts 'Word saved!'
      word.clear()
    elsif letter == 'alphabet' then
      help(@alphabet)
    elsif !@alphabet.has_key?(letter) then
      puts "This letter is not part of the Hiragana Alphabet!"
    else
      word.each do |w| print "#{w}" end # iterating values
    end
    letter = ask()
  end
  puts "Choose an alphabet to start typing:"
  puts "(h) Hiragana (k) Katakana\n\n"
end

Public Instance Methods

ask() click to toggle source
# File lib/hiragana.rb, line 74
def ask() print("\n>"); gets.chomp.downcase end
help(alphabet) click to toggle source
# File lib/hiragana.rb, line 76
def help(alphabet)
  puts "Hiragana Alphabet:"
  alphabet.each do |k, v|
    puts "#{k}: #{v} "
  end
end