# Class X \< Hash ... how do I do X.new { |hash, key| ...}?

**URL:** <https://www.ruby-forum.com/t/class-x-hash-how-do-i-do-x-new-hash-key/113009>\
**Category:** Ruby\
**Created:** [August 26, 2007, 2:06am UTC](https://www.ruby-forum.com/t/class-x-hash-how-do-i-do-x-new-hash-key/113009 "2007-08-26T02:06:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ee59a6/32.png) [@lfast](https://www.ruby-forum.com/u/lfast)\
**Post date:** [August 26, 2007, 2:06am UTC](https://www.ruby-forum.com/t/class-x-hash-how-do-i-do-x-new-hash-key/113009/1 "2007-08-26T02:06:21Z")

</div>

I created a child class of Hash and would like to internalize the  
equivalent of Hash.new {|hash,key| } The following code failed:

class X \< Hash  
def initialize  
super { |hash,key| … }  
end  
end

Any suggestions? I suppose I could do it externally with a create()  
wrapper but it doesn’t seem very pragmatic.

def create\_X  
y = X.new {|hash,key| … }  
return y  
end

My overall goal is to record a list of attempts to read undefined Keys.

Thanks,  
Larry

---

<div class="post-metadata">

**Author:** ![lfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ee59a6/32.png) [@lfast](https://www.ruby-forum.com/u/lfast)\
**Post date:** [August 26, 2007, 2:26am UTC](https://www.ruby-forum.com/t/class-x-hash-how-do-i-do-x-new-hash-key/113009/2 "2007-08-26T02:26:42Z")

</div>

Hi –

On Sun, 26 Aug 2007, Larry F. wrote:

> wrapper but it doesn’t seem very pragmatic.
> 
> def create\_X  
> y = X.new {|hash,key| … }  
> return y  
> end
> 
> My overall goal is to record a list of attempts to read undefined Keys.

Here’s what I did – I’m not sure how it differs from yours but  
hopefully it will help:

class X \< Hash  
attr\_accessor :attempts  
def initialize  
self.attempts = []  
super {|h,k| attempts \<\< k unless h.has\_key?(k); nil }  
end  
end

h = X.new  
h[1] = 2  
p h[1] # 2  
p h[0] # nil  
p h[2] # nil  
p h.attempts # [0,2]

David

---

<div class="post-metadata">

**Author:** ![lfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ee59a6/32.png) [@lfast](https://www.ruby-forum.com/u/lfast)\
**Post date:** [August 26, 2007, 2:32am UTC](https://www.ruby-forum.com/t/class-x-hash-how-do-i-do-x-new-hash-key/113009/3 "2007-08-26T02:32:42Z")

</div>

Hi,

Am Sonntag, 26. Aug 2007, 09:06:24 +0900 schrieb Larry F.:

> I created a child class of Hash and would like to internalize the  
> equivalent of Hash.new {|hash,key| } The following code failed:
> 
> class X \< Hash  
> def initialize  
> super { |hash,key| … }  
> end  
> end
> 
> Any suggestions?

Any error messages?

I pipe the following code from here in the editor into Ruby:

* * *

## class H \< Hash def initialize super { |h,k| h[k] = Time.now } end end h = H.new puts h[:x] sleep 1 puts h[:y] puts h[:x]

## Sun Aug 26 02:30:35 +0200 2007 Sun Aug 26 02:30:36 +0200 2007 Sun Aug 26 02:30:35 +0200 2007

Bertram

---

<div class="post-metadata">

**Author:** ![lfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ee59a6/32.png) [@lfast](https://www.ruby-forum.com/u/lfast)\
**Post date:** [August 26, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/class-x-hash-how-do-i-do-x-new-hash-key/113009/4 "2007-08-26T22:24:26Z")

</div>

Thank you both. Yes, that works. So I’m puzzled by the error I was  
getting. Oh well, carrying on…

I still have a related problem. I’m using YML to export and import my  
Hash. YML loses all the Hash initialization settings. So I may still  
have to create a new internal Hash after reloading from YML.

class X \< Hash  
attr\_accessor :attempts  
def initialize  
self.attempts = []  
super {|h,k| attempts \<\< k unless h.has\_key?(k); nil }  
end  
end

h = X.new  
h[1] = 2  
p h[1] # 2  
p h[0] # nil  
p h[2] # nil  
p h.attempts # [0,2]

yml\_text = YAML.dump(h) # “— !map:X \n1: 2\n”  
j = YAML.load(yml\_text) # {1=\>2}  
j.class # X  
j.attempts # nil  
j[2] = 25 # 25  
j # {1=\>2, 2=\>25}  
j[3] # nil  
j.attempts # nil
