Class: HokkaidoDialect::QuizGame
- Inherits:
-
Object
- Object
- HokkaidoDialect::QuizGame
- Defined in:
- lib/hokkaido_dialect/hokkaido_dialect.rb
Class Method Summary collapse
Instance Method Summary collapse
- #ask_and_check ⇒ Object
- #correct_answer?(input) ⇒ Boolean
-
#initialize(question = QUESTIONS.sample) ⇒ QuizGame
constructor
A new instance of QuizGame.
- #input_until_valid ⇒ Object
- #question_body ⇒ Object
Constructor Details
#initialize(question = QUESTIONS.sample) ⇒ QuizGame
Returns a new instance of QuizGame.
45 46 47 48 49 50 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 45 def initialize(question = QUESTIONS.sample) wrong_usage = question[:wrong_usage] @dialect = question[:dialect] @correct_usage = question[:correct_usage] @choices = [wrong_usage, @correct_usage].shuffle end |
Class Method Details
.all ⇒ Object
34 35 36 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 34 def self.all play_questions(QUESTIONS.shuffle) end |
.play(number_of_questions = 3) ⇒ Object
30 31 32 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 30 def self.play(number_of_questions = 3) play_questions(QUESTIONS.sample(number_of_questions)) end |
.play_questions(questions) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 38 def self.play_questions(questions) results = questions.map do |question| new(question).ask_and_check end puts "#{questions.size}問中#{results.count(true)}問正解!" end |
Instance Method Details
#ask_and_check ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 52 def ask_and_check puts question_body input = input_until_valid correct_answer?(input).tap do |judgment| puts judgment ? '正解!🎉✨🦀' : '不正解…🐄' end end |
#correct_answer?(input) ⇒ Boolean
79 80 81 82 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 79 def correct_answer?(input) index = input - 1 @choices[index] == @correct_usage end |
#input_until_valid ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 66 def input_until_valid loop do print '番号を入力してください(1 or 2): ' input = $stdin.gets.to_i if [1, 2].include?(input) return input end puts '無効な入力です!1または2を入力してください🐻' end end |
#question_body ⇒ Object
62 63 64 |
# File 'lib/hokkaido_dialect/hokkaido_dialect.rb', line 62 def question_body "次の文章で正しい「#{@dialect}」の使い方はどっち?\n1. #{@choices[0]}\n2. #{@choices[1]}" end |