class QuizGame::Problem
Class for one item of quizes. This class does not print to stdout. That should be done by class that deal with interface.
quizId was deplicated. quizStr will work so.
QuizGame
ライブラリでは問題文の構造として以下のようなモデルを考えている。
1. 以下の英単語を日本語単語に訳せ。 (1) dog (2) cat 2. 以下の日本語単語を英単語に訳せ。 (1) 牛 (2) 馬 1. や 2. に続く文は requirement、属する小問全てに対する要求。 (1) や (2) に続くのは question、 実際に対処する要素。 これらに対する解答は answer 正否に関わらない補足情報が supplement
この用語を使う。
requirement を QuizGame::Problem
内では保持しない。 多くの場合、上位の構造で保持して同種の問題を繰り返すものだから。
Attributes
answer[R]
question[R]
supplement[R]
Public Class Methods
new( question, answer, supplement = nil )
click to toggle source
# File lib/quizgame/problem.rb, line 35 def initialize( question, answer, supplement = nil ) @question = question @answer = answer @supplement = supplement end
Public Instance Methods
correct?( data )
click to toggle source
正誤判定。 正誤判定は QuizGame::Problem
内で行う。 QuizGame::Problem
のサブクラスで正誤判定条件を変更できる。 MEMO: 今のところ ignorecase しているが、本来は厳密に一致せんとあかん。
# File lib/quizgame/problem.rb, line 63 def correct?( data ) return true if /^#{@answer}$/i =~ data return false end
emphasize_wrong( str )
click to toggle source
@answer と str を比較して、 正しい部分はデフォルト色、間違った部分は強調色で表示する 文字列を生成。 正しい部分は、先頭から連続して一致する部分と定義。 後ろの一致とかは実装が難しい。 str が @answer より短ければ同じ長さになるように末尾に _ を補う。
# File lib/quizgame/problem.rb, line 74 def emphasize_wrong( str ) index = @answer.mismatch( str ) #index が nil、すなわち @answer と str が完全に一致しているとき、そのまま返す。 return str unless index #差があれば間違い以降を赤字にして返す。 correct_part = str[0..( index - 1 )] wrong_part = str[index .. (-1) ] #str が短ければ同じ長さになるように末尾に _ を補う。 wrong_part += "_" * ( @answer.size - str.size ) if ( @answer.size > str.size ) return ( correct_part + wrong_part.color( :red ) ) end
exhibit_question( io = STDOUT )
click to toggle source
問題文を表示。 QuizGame::Problem
側がこのメソッドを持つことについて。 問題が必ずしも文でないことがあるため。 たとえばヒアリングの問題なんかでは、 QuizGame::Problem
クラスインスタンスがその再生方法などを持っているべきだ。
# File lib/quizgame/problem.rb, line 46 def exhibit_question( io = STDOUT ) io.puts @question end
show_supplement( io = STDOUT )
click to toggle source
problem の supplement があれば、それを出力。 なければ何もせず処理を返す。
# File lib/quizgame/problem.rb, line 52 def show_supplement( io = STDOUT ) return if ( @supplement == nil ) io.puts "[supplement]" io.puts " #{@supplement}" end