# How to use a hash inside a Struct?

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877>\
**Category:** Ruby\
**Created:** [October 22, 2007, 1:16am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877 "2007-10-22T01:16:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jayson](https://www.ruby-forum.com/u/jayson)\
**Post date:** [October 22, 2007, 1:16am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877/1 "2007-10-22T01:16:20Z")

</div>

Is there a way to use a hash inside a Structure. Something like:

object = Struct.new(Hash.new(), :attr2, :attr3)

I know this doesn’t work, but do you get the idea of what I am trying to  
do?

Jayson

---

<div class="post-metadata">

**Author:** ![jayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jayson](https://www.ruby-forum.com/u/jayson)\
**Post date:** [October 22, 2007, 1:21am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877/2 "2007-10-22T01:21:36Z")

</div>

On 10/22/07, Jayson W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Nope. What are you trying to do?

-Daniel

---

<div class="post-metadata">

**Author:** ![jayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jayson](https://www.ruby-forum.com/u/jayson)\
**Post date:** [October 22, 2007, 1:25am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877/3 "2007-10-22T01:25:42Z")

</div>

On 22/10/2007, Jayson W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to use a hash inside a Structure. Something like:
> 
> object = Struct.new(Hash.new(), :attr2, :attr3)

Struct objects except Symbols. What is it you’re trying to do?

– Thomas A.

---

<div class="post-metadata">

**Author:** ![jayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jayson](https://www.ruby-forum.com/u/jayson)\
**Post date:** [October 22, 2007, 1:34am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877/4 "2007-10-22T01:34:28Z")

</div>

I am creating a class that creates elements of a house. For the part  
that creates new rooms, I wanted to link the room name with a room id.  
There may be multiple rooms with the same name, but only one room with  
the unique combination of name and id. I was thinking to use a Hash  
inside the structure definition of the room for the name and id.

---

<div class="post-metadata">

**Author:** ![jayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jayson](https://www.ruby-forum.com/u/jayson)\
**Post date:** [October 22, 2007, 1:37am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877/5 "2007-10-22T01:37:23Z")

</div>

On 10/22/07, Thomas A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:On 22/10/2007,  
Jayson W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to use a hash inside a Structure. Something like:
> 
> object = Struct.new(Hash.new(), :attr2, :attr3)

Do you mean to do something like this?:

Thing = Struct.new(:attr1,:attr2,:attr3)  
t = Thing.new(Hash.new, nil, nil)

---

<div class="post-metadata">

**Author:** ![jayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jayson](https://www.ruby-forum.com/u/jayson)\
**Post date:** [October 22, 2007, 5:40am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877/6 "2007-10-22T05:40:41Z")

</div>

I think both of these methods will work

Thing = Struct.new(:attr1,:attr2,:attr3)  
t = Thing.new(Hash.new, nil, nil)

and…

> require ‘ostruct’
> 
> house = OpenStruct.new  
> house.room = {:room\_id =\> ‘id’, :room\_name =\> ‘name’}  
> house.attr1 = ‘some value’

I was thinking that the attributes in the structure had to be typed  
first (a bit of c left in me). But it makes sense that I can set up  
the attribute and put whatever I want there.  
The second method looks a bit RubyTk’ish. I wonder if RubyTk uses  
OpenStruct.  
Anyway, Thanks. This is just what I was looking for

Jayson

---

<div class="post-metadata">

**Author:** ![jayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jayson](https://www.ruby-forum.com/u/jayson)\
**Post date:** [October 22, 2007, 1:51am UTC](https://www.ruby-forum.com/t/how-to-use-a-hash-inside-a-struct/117877/7 "2007-10-22T01:51:25Z")

</div>

It might be easier to use OpenStruct.

require ‘ostruct’

house = OpenStruct.new  
house.room = {:room\_id =\> ‘id’, :room\_name =\> ‘name’}  
house.attr1 = ‘some value’  
etc…

On 10/22/07, Jayson W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > Nope. What are you trying to do?
> > 
> > -Daniel

–

“Every child has many wishes. Some include a wallet, two chicks and a  
cigar,  
but that’s another story.”
