# Is there a library/mechanism for an object/hash hybrid?

**URL:** <https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666>\
**Category:** Ruby\
**Created:** [March 30, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666 "2007-03-30T20:40:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/1 "2007-03-30T20:40:37Z")

</div>

Hi,

There are times when I want to store items in a hash, and yet refer  
the items  
using class/object method syntax.

For example, I may want to do this:  
myhash[‘aaa’] = ‘bbb’  
and then be able to refer to the ‘aaa’ element either as  
myhash[‘aaa’]  
–or–  
myhash.aaa

I started writing my own class to do this, but I wanted to check to  
see if anyone had  
already done so.

What do you think?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/2 "2007-03-30T20:45:38Z")

</div>

removed\_email\_address@domain.invalid schrieb:

> Hi,
> 
> There are times when I want to store items in a hash, and yet refer  
> the items  
> using class/object method syntax.
> 
> What do you think?  
> [http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html](http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html)

regards  
Jan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/3 "2007-03-30T21:10:41Z")

</div>

On Mar 30, 2007, at 2:45 PM, Jan F. wrote:

> removed\_email\_address@domain.invalid schrieb:
> 
> > Hi,
> > 
> > There are times when I want to store items in a hash, and yet refer  
> > the items  
> > using class/object method syntax.
> > 
> > What do you think?  
> > [http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html](http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html)

OpenStruct doesn’t define access via the indexing method:

info = OpenStruct.new

info.name = ‘Gary’  
info[:name] # undefined method

Gary W.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/4 "2007-03-30T22:15:51Z")

</div>

Gary W. schrieb:

> OpenStruct doesn’t define access via the indexing method:
> 
> info = OpenStruct.new
> 
> info.name = ‘Gary’  
> info[:name] # undefined method  
> It’s Ruby, define it. 🙂  
> class OpenStruct  
> def [] arg  
> self.send arg  
> end

regards,  
Jan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/5 "2007-03-30T21:30:55Z")

</div>

Gary W. wrote:

> OpenStruct doesn’t define access via the indexing method:
> 
> info = OpenStruct.new
> 
> info.name = ‘Gary’  
> info[:name] # undefined method

Hal F. wrote a class called “SuperStruct”. Maybe it’s what you’re  
looking for: [http://sstruct.rubyforge.org/](http://sstruct.rubyforge.org/).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/6 "2007-03-30T21:49:48Z")

</div>

Facet’s OpenObject

gem install facets

require ‘facet/openobject’

oo = OpenObject.new

oo.a = 1

oo[:a] #=\> 1

T.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/7 "2007-03-30T23:24:00Z")

</div>

On Mar 30, 2007, at 4:15 PM, Jan F. wrote:

> Gary W. schrieb:
> 
> > OpenStruct doesn’t define access via the indexing method:
> > 
> > info = OpenStruct.new
> > 
> > info.name = ‘Gary’  
> > info[:name] # undefined method  
> > It’s Ruby, define it. 🙂

Of course. I was just pointing out that OpenStruct didn’t  
actually provide the features that the OP had asked for.

You can always roll your own.

Gary W.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2007, 1:31am UTC](https://www.ruby-forum.com/t/is-there-a-library-mechanism-for-an-object-hash-hybrid/90666/8 "2007-03-31T01:31:20Z")

</div>

On 3/30/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –or–  
> myhash.aaa
> 
> I started writing my own class to do this, but I wanted to check to  
> see if anyone had  
> already done so.

I just read about this in a blog yesterday:

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

The camping framework has a HashWithIndifferentAccess which does this.

Not to be confused with the class HashWithIndifferentAccess in Rails  
(actually in ActiveSupport)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)
