class Foo
include Comparable
attr_reader :size, :color
@@size_order = {
'small' => 1,
'medium' => 2,
'large' => 3,
'super-sized' => 4
}
@@color_order = {
'green' => 1,
'blue' => 2,
'red' => 3
}
def initialize( x, y )
@size = x
@color = y
end
def <=> ( o )
if (@size == o.size)
if (@color == o.color)
return 0
else
return (@@color_order[@color] > @@color_order[o.color]) ? 1 : -1
end
else
return (@@size_order[@size] > @@size_order[o.size]) ? 1 : -1
end
end
end
a = Foo.new( 'small', 'blue' )
b = Foo.new( 'small', 'green' )
c = Foo.new( 'small', 'blue' )
d = Foo.new( 'large', 'red' )
puts a < b
puts a > b
puts a == b
puts '----------'
puts a > c
puts a < c
puts a == c
puts '----------'
puts a > d
puts a < d
puts a == d
Y2xhc3MgRm9vCgogICAgaW5jbHVkZSBDb21wYXJhYmxlCiAgICAKCWF0dHJfcmVhZGVyIDpzaXplLCA6Y29sb3IKCQoJQEBzaXplX29yZGVyID0gewoJCSdzbWFsbCcgPT4gMSwgCgkJJ21lZGl1bScgPT4gMiwgCgkJJ2xhcmdlJyA9PiAzLCAKCQknc3VwZXItc2l6ZWQnID0+IDQKCX0KCQoJQEBjb2xvcl9vcmRlciA9IHsKCQknZ3JlZW4nID0+IDEsCgkJJ2JsdWUnID0+IDIsCgkJJ3JlZCcgPT4gMwoJfQoJCglkZWYgaW5pdGlhbGl6ZSggeCwgeSApCgkJQHNpemUgPSB4CgkJQGNvbG9yID0geQoJZW5kCgkKCWRlZiA8PT4gKCBvICkKCQlpZiAoQHNpemUgPT0gby5zaXplKQoJCQlpZiAoQGNvbG9yID09IG8uY29sb3IpIAoJCQkJcmV0dXJuIDAKCQkJZWxzZQoJCQkJcmV0dXJuIChAQGNvbG9yX29yZGVyW0Bjb2xvcl0gPiBAQGNvbG9yX29yZGVyW28uY29sb3JdKSA/IDEgOiAtMQoJCQllbmQKCQllbHNlCgkJCXJldHVybiAoQEBzaXplX29yZGVyW0BzaXplXSA+IEBAc2l6ZV9vcmRlcltvLnNpemVdKSA/I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