Skip to content

Commit 510de0f

Browse files
author
Tom Johnson
committed
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.
1 parent 378a369 commit 510de0f

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/rdf/model/list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def eql?(other)
452452
# @return [Integer]
453453
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-3C-3D-3E
454454
def <=>(other)
455-
to_a <=> other.to_a # TODO: optimize this
455+
to_a <=> Array(other)
456456
end
457457

458458
##

spec/model_list_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@
571571
it "returns 0 when given the same list" do
572572
expect(ten).to eq ten
573573
end
574+
575+
it "returns 0 when given the same list as array" do
576+
expect(ten).to eq ten.to_a
577+
end
574578
end
575579

576580
describe "#==" do
@@ -591,8 +595,10 @@
591595
expect(ten).not_to eq ten.statements.first
592596
expect(ten).not_to eq RDF::Node.new
593597
expect(ten).not_to eq RDF::Graph.new
598+
expect(ten).not_to eq RDF::Literal.new('')
599+
expect(ten).not_to eq Object.new
594600
end
595-
601+
596602
it "returns false when comparing to similar statements" do
597603
statement = RDF::Statement(:s, :p, :o)
598604
quasistatement = RDF::List[:s, :p, :o]

0 commit comments

Comments
 (0)