Is there a shorter way to compare these 2 objects?

(Kroeger Simon (ext)) #1

From: James Edward Gray II [mailto:james@grayproductions.net]

def <=>( other )
   [@a, @b, @c, @d] <=> [other.a, other.b, other.c, other.d]
end

Arrays are Comparable and they compare each of their contents
in order.

Hope that helps.

James Edward Gray II

not as elegant but without allocating two arrays:

def <=>( other )
  (@a <=> other.a).nonzero? ||
  (@b <=> other.b).nonzero? ||
  (@c <=> other.c).nonzero? ||
  (@d <=> other.d)
end

cheers

Simon

(Lowell Kirsh) #2

Cool, I didn't realize nonzero? returned anything useful other than truth/falsity.

Kroeger Simon (ext) wrote:

···

From: James Edward Gray II [mailto:james@grayproductions.net]

def <=>( other )
  [@a, @b, @c, @d] <=> [other.a, other.b, other.c, other.d]
end

Arrays are Comparable and they compare each of their contents in order.

Hope that helps.

James Edward Gray II

not as elegant but without allocating two arrays:

def <=>( other )
  (@a <=> other.a).nonzero? ||
  (@b <=> other.b).nonzero? ||
  (@c <=> other.c).nonzero? ||
  (@d <=> other.d)
end

cheers

Simon