# Storing Array in Hash

**URL:** <https://www.ruby-forum.com/t/storing-array-in-hash/153945>\
**Category:** Ruby\
**Created:** [November 23, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/storing-array-in-hash/153945 "2008-11-23T15:17:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 23, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/storing-array-in-hash/153945/1 "2008-11-23T15:17:15Z")

</div>

I am having the most insane time with Ruby’s Hash class.

I have Array’s of varied length, but whenever they are stored and then  
retrieved from a Hash they become length of 1 and all the values are  
somehow combined in that first cell.

Code:

aRay = [“a”,“b”,“c”]  
aRay2 = [“1”,“2”,“3”,“4”,“5”]

# prints out 3

puts aRay.size

# prints out 5

puts aRay2.size

# store them

h = {“letters” =\> aRay, “numbers” =\> aRay2}

# prints out abc

puts h.values\_at(“letters”).to\_s

# prints out 12345

puts h.values\_at(“numbers”).to\_s

# retrieve the arrays

ltrArray = h.values\_at(“letters”)  
numArray = h.values\_at(“numbers”)

# prints out abc

puts ltrArray.to\_s

# prints out 12345

puts numArray.to\_s

# prints out 1

puts ltrArray.size

# prints out 1

puts numArray.size

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 23, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/storing-array-in-hash/153945/2 "2008-11-23T15:23:36Z")

</div>

On 23 nov. 08, at 15:13, John B. wrote:

> puts h.values\_at(“letters”).to\_s

Hash#values\_at returns an array of the requested values, so in your  
case you get an array of array(s).

What you want is:  
ltrArray = h.values\_at(“letters”).first  
numArray = h.values\_at(“numbers”).first

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 23, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/storing-array-in-hash/153945/3 "2008-11-23T15:27:08Z")

</div>

Alle Sunday 23 November 2008, John B. ha scritto:

> # prints out 12345
> 
> # prints out 1
> 
> puts ltrArray.size
> 
> # prints out 1
> 
> puts numArray.size
> 
> What am I doing wrong?

Try replacing lines like

puts ltrArray.to\_s

with

p ltrArray

and you’ll understand what’s happening.

By the way, you don’t need to add the .to\_s to the argument of a call to  
puts  
(or p), as puts will do that for you. Also, to display the contents of  
hashes  
and arrays, the inspect (and the associated p) method may be more useful  
than  
the to\_s (and associated puts) method.

Stefano

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 23, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/storing-array-in-hash/153945/4 "2008-11-23T15:51:10Z")

</div>

On 23.11.2008 15:24, John B. wrote:

> Luc H. wrote:
> 
> > What you want is:  
> > ltrArray = h.values\_at(“letters”).first  
> > numArray = h.values\_at(“numbers”).first
> 
> Oh wow…I’ve been plugging away at this for 4 hours now and that just  
> fixed my problem. Thank you so much!
> 
> I’m in the process of learning Ruby through various online mediums so I  
> tend to miss little intricacies like that.

A good tool to try these things out is irb which comes with your Ruby  
distribution. Since irb by default uses #inspect (the same that method  
“p” uses) to print out objects you can immediately see what’s going on.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [November 23, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/storing-array-in-hash/153945/5 "2008-11-23T15:28:29Z")

</div>

Luc H. wrote:

> What you want is:  
> ltrArray = h.values\_at(“letters”).first  
> numArray = h.values\_at(“numbers”).first

Oh wow…I’ve been plugging away at this for 4 hours now and that just  
fixed my problem. Thank you so much!

I’m in the process of learning Ruby through various online mediums so I  
tend to miss little intricacies like that.

Thanks again.
