# Multiple values for the same key in a Hash

**URL:** <https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954>\
**Category:** Ruby\
**Created:** [October 12, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954 "2006-10-12T20:22:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 12, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954/1 "2006-10-12T20:22:14Z")

</div>

Hi. There’s got to be an easy way to do this.

I simply want multiple iteams (etc, price, quantity, and name) appended  
to a key…

There’s got to be a simple way (…right?)

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 12, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954/2 "2006-10-12T20:39:48Z")

</div>

Dominic S. wrote:

> Hi. There’s got to be an easy way to do this.
> 
> I simply want multiple iteams (etc, price, quantity, and name) appended  
> to a key…
> 
> There’s got to be a simple way (…right?)

Uh… Wouldn’t a hash of hashes do the trick?

For example,  
my\_hash[:key][:price] = 1.99  
my\_hash[:key][:quantity] = 5  
…

or

my\_hash[:key] = { :price =\> 1.99, :quantity =\> 5 }

or whatever the syntax is.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 12, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954/3 "2006-10-12T20:55:42Z")

</div>

Yes, i was thinking your later suggestion, but how do i iterate through  
them…

the price, and quanity come back too containated (ie : 1.995 ) do i have  
to mess with a .split and reg exp at this point? please say it isn’t so!

Robert H. wrote:

> Dominic S. wrote:
> 
> > Hi. There’s got to be an easy way to do this.
> > 
> > I simply want multiple iteams (etc, price, quantity, and name) appended  
> > to a key…
> > 
> > There’s got to be a simple way (…right?)
> 
> Uh… Wouldn’t a hash of hashes do the trick?
> 
> For example,  
> my\_hash[:key][:price] = 1.99  
> my\_hash[:key][:quantity] = 5  
> …
> 
> or
> 
> my\_hash[:key] = { :price =\> 1.99, :quantity =\> 5 }
> 
> or whatever the syntax is.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 18, 2006, 10:38am UTC](https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954/4 "2006-10-18T10:38:58Z")

</div>

Dominic S. wrote:

> Hi. There’s got to be an easy way to do this.
> 
> I simply want multiple iteams (etc, price, quantity, and name) appended  
> to a key…
> 
> There’s got to be a simple way (…right?)
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Just add the items as an array or another container object.

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 18, 2006, 10:40am UTC](https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954/5 "2006-10-18T10:40:58Z")

</div>

On 12.10.2006 20:22, Dominic S. wrote:

> Hi. There’s got to be an easy way to do this.
> 
> I simply want multiple iteams (etc, price, quantity, and name) appended  
> to a key…
> 
> There’s got to be a simple way (…right?)

Like this?

> > 11:09:28 [~]: irbs  
> > require ‘pp’  
> > =\> true
> 
> > Entry = Struct.new(:price, :quantity, :name)  
> > =\> Entry
> 
> > h = Hash.new {|h,k| h[k] = Entry.new}  
> > =\> {}
> 
> > h[:foo].price = 10  
> > =\> 10
> 
> > h[:bar].name = “bar”  
> > =\> “bar”
> 
> > h[:foo].name = “foo”  
> > =\> “foo”
> 
> > pp h  
> > {:bar=\>#,  
> > :foo=\>#}  
> > =\> nil

```
robert
```

---

<div class="post-metadata">

**Author:** ![eddiewould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@eddiewould](https://www.ruby-forum.com/u/eddiewould)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/multiple-values-for-the-same-key-in-a-hash/71954/6 "2006-10-18T10:44:06Z")

</div>

Robert K. wrote:

> On 12.10.2006 20:22, Dominic S. wrote:
> 
> > Hi. There’s got to be an easy way to do this.
> > 
> > I simply want multiple iteams (etc, price, quantity, and name)  
> > appended to a key…
> > 
> > There’s got to be a simple way (…right?)
> 
> Like this?

PS: Alternatively you can use OpenStruct.

```
robert
```
