Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.64 KB | None | 0 0
  1. class A
  2.   def initialize a
  3.     @arr = a
  4.   end
  5.   def get i
  6.     @arr[i]
  7.   end
  8.   def sum
  9.     @arr.inject(0) {|acc,x| acc + x}
  10.   end
  11. end
  12.  
  13. class B < A
  14.   def initialize a
  15.     super
  16.   end
  17.   def sum
  18.     @and ||= super
  19.   end
  20. end
  21.  
  22.  
  23. v = [4,19,74]
  24. a = A.new v
  25. b = B.new v
  26. s1 = a.sum
  27. s2 = b.sum
  28. ...
  29. s3 = a.sum
  30. s4 = b.sum
  31.  
  32. Have the initialize method in class A store a copy of its argument in @arr.            
  33. Remove the method get from class A.     ✘     0.00    
  34. Change the sum method in both classes to use an explicit loop instead of inject and a block.           
  35. Change class A to use a class variable @@arr in place of the instance variable @arr.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement