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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -