Ruby on rails 如何访问和比较两个阵列

Ruby on rails 如何访问和比较两个阵列,ruby-on-rails,arrays,ruby,Ruby On Rails,Arrays,Ruby,通过这些行,我得到了两个数组: @correct_answer = Question.pluck(:correct_answer) # => [1, 1, 2, 2, 1, 1, 3, 1, 3] @selected_answer = Question.pluck(:selected_answer) # => [1, 4, 3, 1, 1, 3, 4, 1, 1] 如何逐一比较这两个数组中的值?我使用以下代码: if @correct_answer[0] == @selected_

通过这些行,我得到了两个数组:

@correct_answer = Question.pluck(:correct_answer)
# => [1, 1, 2, 2, 1, 1, 3, 1, 3]
@selected_answer = Question.pluck(:selected_answer)
# => [1, 4, 3, 1, 1, 3, 4, 1, 1]
如何逐一比较这两个数组中的值?我使用以下代码:

if @correct_answer[0] == @selected_answer[0]
  @result += 1
else
  @result -= 1
end   
correct_answer = [1, 1, 2, 2, 1, 1, 3, 1, 3]
# => [1, 1, 2, 2, 1, 1, 3, 1, 3]
selected_answer = [1, 4, 3, 1, 1, 3, 4, 1, 1]
# => [1, 4, 3, 1, 1, 3, 4, 1, 1]
correct_answer.zip(selected_answer).map do |correct, selected| 
  correct == selected ? 'Correct!' : "Wrong! Correct answer is: #{correct}" 
end
# => ["Correct!", "Wrong! Correct answer is: 1", "Wrong! Correct answer is: 2", "Wrong! Correct answer is: 2", "Correct!", "Wrong! Correct answer is: 1", "Wrong! Correct answer is: 3", "Correct!", "Wrong! Correct answer is: 3"]
但这不起作用。

你可以试试这个

@selected_answers = [1, 4, 3, 1, 1, 3, 4, 1, 1]
@correct_answers = [1, 1, 2, 2, 1, 1, 3, 1, 3]

## This will return count of all matched and unmatched values.
@selected_answers.each_with_index do |selected_answer, index|
  if selected_answer == @correct_answers[index]
    @result += 1
  else
    @result -= 1
  end
end

## This will return count of only matched values.
@selected_answers.each_with_index do |selected_answer, index|
  @result += 1 if selected_answer == @correct_answers[index]
end

这里有一个可能的解决方案

a1 = [1, 1, 2, 2, 1, 1, 3, 1, 3]
a2 = [1, 4, 3, 1, 1, 3, 4, 1, 1]

a1.zip(a2).inject(0) do |result, (correct, selected)|
  result += (correct == selected ? 1 : -1)
end
# => -3 
解释如下:

  • 使用
    zip
    可以将
    a1
    a1
    合并到一个数组中
  • 使用
    inject
    从基值0开始映射和减少数组
  • 如果值匹配,则添加+1,否则添加-1

编辑:您可以通过以下方式计算正确答案:

correct_score = correct_answer.zip(selected_answer).count { |correct, wrong| correct == wrong }
# => 3
wrong_score = selected_answer.size - correct_score
# => 6
total_score = correct_score - wrong_score
# => -3
您还可以找到与此代码匹配的值:

if @correct_answer[0] == @selected_answer[0]
  @result += 1
else
  @result -= 1
end   
correct_answer = [1, 1, 2, 2, 1, 1, 3, 1, 3]
# => [1, 1, 2, 2, 1, 1, 3, 1, 3]
selected_answer = [1, 4, 3, 1, 1, 3, 4, 1, 1]
# => [1, 4, 3, 1, 1, 3, 4, 1, 1]
correct_answer.zip(selected_answer).map do |correct, selected| 
  correct == selected ? 'Correct!' : "Wrong! Correct answer is: #{correct}" 
end
# => ["Correct!", "Wrong! Correct answer is: 1", "Wrong! Correct answer is: 2", "Wrong! Correct answer is: 2", "Correct!", "Wrong! Correct answer is: 1", "Wrong! Correct answer is: 3", "Correct!", "Wrong! Correct answer is: 3"]

多亏了Amit sharma,这段代码才为我工作。

除非您提供了检索值的顺序,否则这段代码毫无意义,不是吗?您希望从这段代码中得到什么结果?我希望当值相等时,在结果中加+1,否则在结果中加-1按正确的顺序“我想遍历其中一个数组,检查另一个数组中相同位置的值是否等于它,…”未定义的方法`each_with_index'表示nil:nilclass请粘贴您的
@selected_answers
value@selected_answers = [1, 4, 3, 1, 1, 3, 4, 1, 1]如果您在rails控制台上执行,请粘贴您的代码预期的输出是什么?但这只会给我0个输出可能更容易分离映射并减少步骤,即
a1.zip(a2).map{x,y | x==y?1:-1}.inject(:+)
我想一个接一个地将每个元素与每个other@jasssingh该代码有效地逐个比较了每个结果。它不会将0作为输出。请尝试我发布的代码。还要尝试从中学习,不要盲目地复制解决方案。相反,复制一个答案,你应该向上选择有用的答案,并将答案标记为better符合您的问题,因为它已被接受。