Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A
- def initialize a
- @arr = a
- end
- def get i
- @arr[i]
- end
- def sum
- @arr.inject(0) {|acc,x| acc + x}
- end
- end
- class B < A
- def initialize a
- super
- end
- def sum
- @and ||= super
- end
- end
- v = [4,19,74]
- a = A.new v
- b = B.new v
- s1 = a.sum
- s2 = b.sum
- ...
- s3 = a.sum
- s4 = b.sum
- Have the initialize method in class A store a copy of its argument in @arr.
- Remove the method get from class A. ✘ 0.00
- Change the sum method in both classes to use an explicit loop instead of inject and a block.
- 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