Skip to content

Commit

Permalink
Cleanup and compliancy fixes for Array#<=>
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Oct 28, 2013
1 parent 5fec442 commit 23393da
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions corelib/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,27 @@ def <<(object)
end

def <=>(other)
if Array === other
other = other.to_a
elsif other.respond_to? :to_ary
other = other.to_ary
else
return
end

%x{
if (#{self.hash} === #{other.hash}) {
if (#{hash} === #{other.hash}) {
return 0;
}
if (#{self}.length != other.length) {
return (#{self}.length > other.length) ? 1 : -1;
if (self.length != other.length) {
return (self.length > other.length) ? 1 : -1;
}
for (var i = 0, length = #{self}.length, tmp; i < length; i++) {
if ((tmp = #{`#{self}[i]` <=> `other[i]`}) !== 0) {
for (var i = 0, length = self.length; i < length; i++) {
var tmp = #{`self[i]` <=> `other[i]`};
if (tmp !== 0) {
return tmp;
}
}
Expand Down

0 comments on commit 23393da

Please sign in to comment.