結果:
Failures:
1) An usual sorter sorts downwards by default
Failure/Error: [a,b,c,d,e,f,g].sort.should == [7,6,5,5,4,3,3]
expected: [7, 6, 5, 5, 4, 3, 3]
got: [7, 6, 5, 5, 4, 3, 3] (using ==)
# ./downsort_spec.rb:13:in `block (2 levels) in <top (required)>'
Finished in 0.00077 seconds
テスト:
require_relative 'my_sorter.rb'
describe "A usual sorter" do
it "sorts downwards by default" do
my_array= [3,5,7,5,3,6,4,2,5,6]
a=MySorter.new(3)
b=MySorter.new(5)
c=MySorter.new(7)
d=MySorter.new(5)
e=MySorter.new(3)
f=MySorter.new(6)
g=MySorter.new(4)
[a,b,c,d,e,f,g].sort.should == [7,6,5,5,4,3,3]
end
end
コード:
class MySorter
include Comparable
attr_reader :value
def initialize(value)
@value = value
end
def <=> (other)
if value > other.value then
-1
elsif value < other.value then
1
else
0
end
end
def inspect
@value
end
end
今のところ非常に単純な並べ替えがありますが、これが機能すると、意図はより複雑になります (したがって、比較方法の詳細)。