开发者

Extend Hash class

开发者 https://www.devze.com 2022-12-15 11:40 出处:网络
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:

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
   e开发者_开发百科nd

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

What do I need to put instead @something@ ?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消