sorting - Ruby sort - why rspec error when "expected: [7, 6, 5, 5, 4, 3, 3]" seems the same as "got: [7, 6, 5, 5, 4, 3, 3]"? -


result:

failures:    1) usual sorter sorts downwards 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 

test:

require_relative 'my_sorter.rb'  describe "a usual sorter"   "sorts downwards default"     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 

code:

class mysorter   include comparable   attr_reader :value    def initialize(value)     @value = value   end    def <=> (other)     if value > other.value       -1     elsif value < other.value       1     else        0     end   end    def inspect     @value   end  end 

i have simple sort now, intent more complex 1 once have working (hence detail in comparison method).

you comparing array of mysorter objects array of fixnums. need change this:

[a,b,c,d,e,f,g].sort.should == [7,6,5,5,4,3,3] 

to

[a,b,c,d,e,f,g].sort.map(&:value).should == [7,6,5,5,4,3,3] 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -