Class: Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/quizgame/problem.rb

Overview

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
  1. 以下の日本語単語を英単語に訳せ。

(1) 牛
(2) 馬
  1. や 2. に続く文は requirement、属する小問全てに対する要求。

(1) や (2) に続くのは question、 実際に対処する要素。 これらに対する解答は answer 正否に関わらない補足情報が supplement この用語を使う。

requirement を Problem 内では保持しない。 多くの場合、上位の構造で保持して同種の問題を繰り返すものだから。

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question, answer, supplement = nil) ⇒ Problem

Returns a new instance of Problem.



35
36
37
38
39
# File 'lib/quizgame/problem.rb', line 35

def initialize( question, answer, supplement = nil )
  @question    = question
  @answer      = answer
  @supplement  = supplement
end

Instance Attribute Details

#answerObject (readonly)

Returns the value of attribute answer.



33
34
35
# File 'lib/quizgame/problem.rb', line 33

def answer
  @answer
end

#questionObject (readonly)

Returns the value of attribute question.



33
34
35
# File 'lib/quizgame/problem.rb', line 33

def question
  @question
end

#supplementObject (readonly)

Returns the value of attribute supplement.



33
34
35
# File 'lib/quizgame/problem.rb', line 33

def supplement
  @supplement
end

Instance Method Details

#correct?(data) ⇒ Boolean

正誤判定。 正誤判定は Problem 内で行う。 Problem のサブクラスで正誤判定条件を変更できる。 MEMO: 今のところ ignorecase しているが、本来は厳密に一致せんとあかん。

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/quizgame/problem.rb', line 63

def correct?( data )
  return true if /^#{@answer}$/i =~ data
  return false
end

#emphasize_wrong(str) ⇒ Object

正しい部分はデフォルト色、間違った部分は強調色で表示する 文字列を生成。 正しい部分は、先頭から連続して一致する部分と定義。 後ろの一致とかは実装が難しい。 str が @answer より短ければ同じ長さになるように末尾に _ を補う。



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object

問題文を表示。 Problem 側がこのメソッドを持つことについて。 問題が必ずしも文でないことがあるため。 たとえばヒアリングの問題なんかでは、 Problem クラスインスタンスがその再生方法などを持っているべきだ。



46
47
48
# File 'lib/quizgame/problem.rb', line 46

def exhibit_question( io = STDOUT )
  io.puts @question
end

#show_supplement(io = STDOUT) ⇒ Object

problem の supplement があれば、それを出力。 なければ何もせず処理を返す。



52
53
54
55
56
57
# File 'lib/quizgame/problem.rb', line 52

def show_supplement( io = STDOUT )
  return if ( @supplement == nil )

  io.puts "[supplement]"
  io.puts "  #{@supplement}"
end