# OpenHash class I thought I would share

**URL:** <https://www.ruby-forum.com/t/openhash-class-i-thought-i-would-share/121515>\
**Category:** Rails\
**Created:** [November 28, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/openhash-class-i-thought-i-would-share/121515 "2007-11-28T16:44:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![uncle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e5b9ba/32.png) [@uncle](https://www.ruby-forum.com/u/uncle)\
**Post date:** [November 28, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/openhash-class-i-thought-i-would-share/121515/1 "2007-11-28T16:44:01Z")

</div>

I love OpenStruct, but it has no ability to iterate over the members.  
I had extended it, but recently I use this little class. Anyone see  
any issues with it ?

class OpenHash \< Hash  
def method\_missing(name, value=nil)  
key = name.to\_s.sub(/[=?!]$/,’’).to\_sym  
self[key] = value if name.to\_s[-1,1] == “=”  
return self[key]  
end  
end

> > o = OpenHash.new  
> > =\> {}
> 
> > o.a = 1  
> > =\> 1
> 
> > o.b = 2  
> > =\> 2
> 
> > o.h = OpenHash.new  
> > =\> {}
> 
> > o.h.a = 1  
> > =\> 1
> 
> > o.h.b = 2  
> > =\> 2
> 
> > pp o  
> > {:a=\>1, :b=\>2, :h=\>{:a=\>1, :b=\>2}}  
> > =\> nil
