レール、ルビーの配列のキータイプに基づいて新しい配列を生成し<code>["A", "B", "C", "D", "E", "F"]</code>として、今私は、定義された順序での質問のゲームの特定のセット(32の質問)を生成する必要があり、私は質問タイプとの質問に設定した
のようなゲーム1 ["A", "A", "C", "A", "D", "A", "C", "D", "A", "A", "E", "F", .. ]
のためにこれまでのところ私は
def self.generate_rps_question(game_id)
questions = []
q1 = Question.where(game_id: game_id).where(qtype: "A").sample(1)
questions << q1 unless questions.include? (q1)
q2 = Question.where(game_id: game_id).where(qtype: "A").sample(1)
questions << q2 unless questions.include? (q2)
q3 = Question.where(game_id: game_id).where(qtype: "C").sample(1)
questions << q3 unless questions.include? (q3)
q4 = Question.where(game_id: game_id).where(qtype: "A").sample(1)
questions << q4 unless questions.include? (q4)
q5 = Question.where(game_id: game_id).where(qtype: "D").sample(1)
questions << q5 unless questions.include? (q5)
.
.
.
.
questions
end
これを行うには良い(短い)方法はありますが、次の行っていますか?
更新
def self.generate_rps_question(game_id, types)
types.inject([]) do |memo, type|
unless type == "F"
while memo.include?(
q = Question.where(game_id: game_id, qtype: type).sample) do end # skip unless unique
else
q = Question.where(game_id: game_id, qtype: type).sample
end
memo << q
end
end
は、質問タイプの事前に定義された順序からの質問を選択しますか、またはあなたがNEありません質問タイプの順番をランダム化することもできますか? –
@RohitJangid質問タイプの順序があらかじめ定義されています。 –