ceth
ceth

Reputation: 45325

Extend Hash class

I need to create my own class that extends Hash and append the additional method to insert to hash my own class variables. Something like this:

   class MyHash < Hash
     def initialize
       super
       @local = 0
     end

     def append_my_data(my_data)
       @something@[my_data.id] = my_data
     end
   end

   d = MyHash.new
   d.append_my_data(some_var)
   p d[10]

What do I need to put instead @something@ ?

Upvotes: 2

Views: 7958

Answers (1)

DataWraith
DataWraith

Reputation: 3414

self[my_data.id] = my_data should do the trick. See http://rubylearning.com/satishtalim/ruby_self.html.

Upvotes: 7

Related Questions