Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. class Hero
  2. public
  3. #redefine comparison like this just for the sake of having it use protected method
  4. def <=> hero
  5. @mass <=> hero.get_mass
  6. end
  7.  
  8. def initialize mass
  9. @mass = mass
  10. end
  11.  
  12. protected
  13. #this protected method is used for inheritance testing purposes
  14. def get_mass
  15. @mass
  16. end
  17.  
  18. end
  19.  
  20. class Finn < Hero
  21.  
  22. def initialize mass, sword_length
  23. @mass = mass
  24. @sword_length = sword_length
  25. end
  26. # THIS! Am I doing this right?
  27. def <=> finn
  28. if finn.class == self.class
  29. (@mass+@sword_length) <=> (finn.get_mass+finn.get_sword_length)
  30. else
  31. super(finn)
  32. end
  33. end
  34. # probably not...
  35. protected
  36.  
  37. def get_sword_length
  38. @sword_length
  39. end
  40. end
  41.  
  42. class Jake < Hero
  43.  
  44. def initialize mass, fist_length
  45. @mass = mass
  46. @fist_length = fist_length
  47. end
  48.  
  49. def <=> jake
  50. if jake.class == self.class
  51. (@mass+@fist_length) <=> (jake.get_mass+jake.get_fist_length)
  52. else
  53. super(jake)
  54. end
  55. end
  56.  
  57. protected
  58.  
  59. def get_fist_length
  60. @fist_length
  61. end
  62.  
  63. end
  64.  
  65. hero1 = Finn.new(10, 80)
  66. hero4 = Jake.new(50, 18)
  67. puts " error? "+(hero1<=>hero4).to_s
  68.  
  69. class Hero
  70. def initialize(mass)
  71. @mass = mass
  72. end
  73.  
  74. def <=>(hero)
  75. mass <=> hero.mass
  76. end
  77.  
  78. protected #I put this as protected since you did, but I would likely leave it public
  79.  
  80. attr_reader :mass
  81. end
  82.  
  83.  
  84. class Finn < Hero
  85. def initialize(mass, sword_length)
  86. @mass = mass
  87. @sword_length = sword_length
  88. end
  89.  
  90. def <=>(finn)
  91. if finn.respond_to? :sword_length
  92. (mass + sword_length) <=> (finn.mass + finn.sword_length)
  93. else
  94. super(finn)
  95. end
  96. end
  97.  
  98. protected
  99.  
  100. attr_reader :sword_length
  101. end
  102.  
  103.  
  104. class Jake < Hero
  105.  
  106. def initialize(mass, fist_length)
  107. @mass = mass
  108. @fist_length = fist_length
  109. end
  110. #edited
  111. def <=>(jake)
  112. if jake.respond_to? :fist_length
  113. (mass + fist_length) <=> (jake.mass + jake.fist_length)
  114. else
  115. super(jake)
  116. end
  117. end
  118.  
  119. protected
  120.  
  121. attr_reader :fist_length
  122. end
  123.  
  124. class Hero
  125. attr_reader :mass
  126. # more info in attr_reader and attr_accessor is here http://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer
  127.  
  128. def initialize mass
  129. @mass = mass
  130. end
  131.  
  132. def <=> hero
  133. mass <=> hero.mass
  134. end
  135. end
  136.  
  137. class Finn < Hero
  138. attr_reader :sword_length
  139.  
  140. def initialize mass, sword_length
  141. @sword_length = sword_length
  142. super(mass)
  143. end
  144.  
  145. def <=> finn
  146. if finn.class == self.class
  147. (@mass+@sword_length) <=> (finn.mass+finn.sword_length)
  148. else
  149. super(finn)
  150. end
  151. end
  152. end
  153.  
  154. class Jake < Hero
  155. attr_reader :fist_length
  156.  
  157. def initialize mass, fist_length
  158. @fist_length = fist_length
  159. super(mass)
  160. end
  161.  
  162. def <=> jake
  163. #alternate dense way to do it
  164. jake.class == self.class ? ((@mass+@fist_length) <=> (jake.mass+jake.fist_length)) : super(jake)
  165. end
  166.  
  167. end
  168.  
  169. hero1 = Finn.new(10, 80)
  170. hero4 = Jake.new(50, 18)
  171. puts " error? "+(hero1<=>hero4).to_s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement