From 510de0f7814c583ef519d42c9fa1d1a0fdf0c061 Mon Sep 17 00:00:00 2001 From: Tom Johnson Date: Thu, 30 Jun 2016 07:49:47 -0700 Subject: [PATCH] Adjust `List#<=>` to avoid error cases Using `Array(other)` instead of `other.to_a` avoids throwing errors when comparing. This minor tweak slightly improves the solution to #304 given in #305. --- lib/rdf/model/list.rb | 2 +- spec/model_list_spec.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/rdf/model/list.rb b/lib/rdf/model/list.rb index e702c10d..0d38729e 100644 --- a/lib/rdf/model/list.rb +++ b/lib/rdf/model/list.rb @@ -452,7 +452,7 @@ def eql?(other) # @return [Integer] # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-3C-3D-3E def <=>(other) - to_a <=> other.to_a # TODO: optimize this + to_a <=> Array(other) end ## diff --git a/spec/model_list_spec.rb b/spec/model_list_spec.rb index 37fcd5ad..78e16b7e 100644 --- a/spec/model_list_spec.rb +++ b/spec/model_list_spec.rb @@ -571,6 +571,10 @@ it "returns 0 when given the same list" do expect(ten).to eq ten end + + it "returns 0 when given the same list as array" do + expect(ten).to eq ten.to_a + end end describe "#==" do @@ -591,8 +595,10 @@ expect(ten).not_to eq ten.statements.first expect(ten).not_to eq RDF::Node.new expect(ten).not_to eq RDF::Graph.new + expect(ten).not_to eq RDF::Literal.new('') + expect(ten).not_to eq Object.new end - + it "returns false when comparing to similar statements" do statement = RDF::Statement(:s, :p, :o) quasistatement = RDF::List[:s, :p, :o]