# Adding to Hashes

**URL:** <https://rubytalk.org/t/adding-to-hashes/35007>\
**Category:** ruby-talk\
**Created:** [2 February 2007 10:56 UTC](https://rubytalk.org/t/adding-to-hashes/35007 "2007-02-02T10:56:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Black](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@Nick\_Black](https://rubytalk.org/u/Nick_Black)\
**Post date:** [2 February 2007 10:56 UTC](https://rubytalk.org/t/adding-to-hashes/35007/1 "2007-02-02T10:56:25Z")

</div>

Hello,

I need to add key and value pairs to a hash dynamically. I want to do this:

&nbsp;&nbsp;array = []  
&nbsp;&nbsp;array \<\< "hello"  
&nbsp;&nbsp;array \<\< "world"

But with k v pairs of a hash. The hash is being populated from a  
database and there is a potenitally unlimited (though realistically  
limited to less than 20) number of differnet keys, which my program  
will not know about until it talks to the database.

Can I dynamically create a hash something like this:

dbh.query("select k, v from foo where id = #{id}").each\_hash do |kv|

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k = kv['k']  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = kv['v']

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myhash \<\< k,v  
end

return myhash

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [2 February 2007 11:04 UTC](https://rubytalk.org/t/adding-to-hashes/35007/2 "2007-02-02T11:04:37Z")

</div>

> Hello,
> 
> I need to add key and value pairs to a hash dynamically. I want to do this:
> 
> &nbsp;&nbsp;array =   
> &nbsp;&nbsp;array \<\< "hello"  
> &nbsp;&nbsp;array \<\< "world"
> 
> But with k v pairs of a hash. The hash is being populated from a  
> database and there is a potenitally unlimited (though realistically  
> limited to less than 20) number of differnet keys, which my program  
> will not know about until it talks to the database.
> 
> Can I dynamically create a hash something like this:
> 
> dbh.query("select k, v from foo where id = #{id}").each\_hash do |kv|
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k = kv['k']  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = kv['v']
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myhash \<\< k,v

Why not just  
&nbsp;&nbsp;&nbsp;myhash[k] = v

> **···**
>
> On 2/2/07, Nick Black \<nickblack1@gmail.com\> wrote:
> 
> > end
> > 
> > return myhash

---

<div class="post-metadata">

**Author:** ![Nick\_Black](https://avatars.discourse-cdn.com/v4/letter/n/e9a140/32.png) [@Nick\_Black](https://rubytalk.org/u/Nick_Black)\
**Post date:** [2 February 2007 15:35 UTC](https://rubytalk.org/t/adding-to-hashes/35007/3 "2007-02-02T15:35:52Z")

</div>

Figured it out - its simple as always:

yourhash['key'] = value

adds to the hash (just like the book says)

> **···**
>
> On 2/2/07, Nick Black \<nickblack1@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I need to add key and value pairs to a hash dynamically. I want to do this:
> > 
> > &nbsp;&nbsp;array =   
> > &nbsp;&nbsp;array \<\< "hello"  
> > &nbsp;&nbsp;array \<\< "world"
> > 
> > But with k v pairs of a hash. The hash is being populated from a  
> > database and there is a potenitally unlimited (though realistically  
> > limited to less than 20) number of differnet keys, which my program  
> > will not know about until it talks to the database.
> > 
> > Can I dynamically create a hash something like this:
> > 
> > dbh.query("select k, v from foo where id = #{id}").each\_hash do |kv|
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k = kv['k']  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = kv['v']
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myhash \<\< k,v  
> > end
> > 
> > return myhash
> > 
> > Thanks for the help!
> 
> --  
> Nick Black  
> --------------------------------  
> [http://www.blacksworld.net](http://www.blacksworld.net)
