How to make myself def class act as array in ruby? -
class heap attr_accessor :a, :heap_size def initialnize(a, heap_size) @a = @heap_size = heap_size end end = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] = heap.new(a, a.length-1)
what should do? can aceess 16 use a[i],etc.
you can use inheritance:
class heap < array attr_accessor :heap_size def initialize(a, heap_size) @heap_size = heap_size super(a) end end = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] heap = heap.new(a, a.length-1) heap[0] # => 16
Comments
Post a Comment