fork download
  1. class Foo
  2.  
  3. include Comparable
  4.  
  5. attr_reader :size, :color
  6.  
  7. @@size_order = {
  8. 'small' => 1,
  9. 'medium' => 2,
  10. 'large' => 3,
  11. 'super-sized' => 4
  12. }
  13.  
  14. @@color_order = {
  15. 'green' => 1,
  16. 'blue' => 2,
  17. 'red' => 3
  18. }
  19.  
  20. def initialize( x, y )
  21. @size = x
  22. @color = y
  23. end
  24.  
  25. def <=> ( o )
  26. if (@size == o.size)
  27. if (@color == o.color)
  28. return 0
  29. else
  30. return (@@color_order[@color] > @@color_order[o.color]) ? 1 : -1
  31. end
  32. else
  33. return (@@size_order[@size] > @@size_order[o.size]) ? 1 : -1
  34. end
  35. end
  36.  
  37. end
  38.  
  39. a = Foo.new( 'small', 'blue' )
  40. b = Foo.new( 'small', 'green' )
  41. c = Foo.new( 'small', 'blue' )
  42. d = Foo.new( 'large', 'red' )
  43.  
  44.  
  45. puts a < b
  46. puts a > b
  47. puts a == b
  48. puts '----------'
  49. puts a > c
  50. puts a < c
  51. puts a == c
  52. puts '----------'
  53. puts a > d
  54. puts a < d
  55. puts a == d
  56.  
Success #stdin #stdout 0.02s 7460KB
stdin
Standard input is empty
stdout
false
true
false
----------
false
false
true
----------
false
true
false