结果:
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
我现在有一个非常简单的排序,一旦我完成了这个工作,意图将变得更加复杂(因此比较方法中的细节)。