为什么uniq在另一个对象中使用时会返回一个不合逻辑的答案?(Why does uniq return an illogical answer when used inside another obj

编程入门 行业动态 更新时间:2024-10-08 06:24:52
为什么uniq在另一个对象中使用时会返回一个不合逻辑的答案?(Why does uniq return an illogical answer when used inside another object?)

感谢您的时间。 我的RSpec规范中有一个有趣的逻辑错误。 下面我提供了两个例子,第一个例子总是失败,而第二个传递。

例如:下面的代码将创建一个期望为3但是返回1的失败规范

it "must not have the same question twice" do exam = Exam.new q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.questions.push(q1,q2,q3,q4,q5) expect(exam.questions.uniq.size).to eql(3) end

但是当我使用下面的代码时,用常规数组替换Exam对象,它按预期工作并传递规范。

it "must not have the same question twice" do exam = [] q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.push(q1,q2,q3,q4,q5) expect(exam.uniq.size).to eql(3) end

以下是我的问题对象中的overrode方法示例

def ==(other) question == other.question end def eql?(other) other.is_a?(self.class) && question.eql?(other.question) end def hash question.hash end

Thank you for your time. I have an interesting logical error in my RSpec specification. Below I have provided two examples, the first one always fails whilst the second passes.

For example: The below code will create a failing specification expecting 3 but getting back 1

it "must not have the same question twice" do exam = Exam.new q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.questions.push(q1,q2,q3,q4,q5) expect(exam.questions.uniq.size).to eql(3) end

However when I use the below, replacing the Exam object with a regular array it works as intended and passes the specification.

it "must not have the same question twice" do exam = [] q1 = Question.new(question: "What color is the sky?") q2 = Question.new(question: "What color is the sky?") q3 = Question.new(question: "What sound does a dog make?") q4 = Question.new(question: "What sound does a dog make?") q5 = Question.new(question: "hello world?") exam.push(q1,q2,q3,q4,q5) expect(exam.uniq.size).to eql(3) end

Here is a sample of my overrode methods inside my question object

def ==(other) question == other.question end def eql?(other) other.is_a?(self.class) && question.eql?(other.question) end def hash question.hash end

最满意答案

exam.questions.uniq正在删除ActiveRecord :: Associations :: CollectionProxy的重复项。

exam.uniq正在删除数组的重复项。

如果查看ActiveRecord 源,您可以看到记录ID用于确定唯一性而不是相等性。

def distinct seen = {} load_target.find_all do |record| seen[record.id] = true unless seen.key?(record.id) end end alias uniq distinct

exam.questions.uniq is removing duplicates for an ActiveRecord::Associations::CollectionProxy.

exam.uniq is removing duplicates for an Array.

If you look at the ActiveRecord source you can see that record ID is used to determine uniqueness instead of equality.

def distinct seen = {} load_target.find_all do |record| seen[record.id] = true unless seen.key?(record.id) end end alias uniq distinct

更多推荐

本文发布于:2023-08-07 13:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时会   象中   答案   不合逻辑   uniq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!