# Hash#to\_a method not performing

**URL:** <https://www.ruby-forum.com/t/hash-to-a-method-not-performing/118824>\
**Category:** Rails\
**Created:** [October 31, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/hash-to-a-method-not-performing/118824 "2007-10-31T12:17:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [October 31, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/hash-to-a-method-not-performing/118824/1 "2007-10-31T12:17:03Z")

</div>

Hi  
I need to turn a hash into an array. The method .to\_a should do it.  
unfortunately not

ha = { “c” =\> 300, “a” =\> 100, “d” =\> 400, “c” =\> 300 }  
logger.info('hash ’ + ha.inspect)  
ha.to\_a  
logger.info('array ’ + ha.inspect)

development.log :-  
hash {“a”=\>100, “c”=\>300, “d”=\>400}  
array {“a”=\>100, “c”=\>300, “d”=\>400}

IRB does not show this problem

Ruby Version 1.8.6

Any ideas?

Peter

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [October 31, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/hash-to-a-method-not-performing/118824/2 "2007-10-31T13:19:39Z")

</div>

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

> Hi  
> I need to turn a hash into an array. The method .to\_a should do it.  
> unfortunately not
> 
> ha = { “c” =\> 300, “a” =\> 100, “d” =\> 400, “c” =\> 300 }  
> logger.info('hash ’ + ha.inspect)  
> ha.to\_a

This creates a new object which is the result of sending to\_a to the  
object referenced by ha, then throws it away.

> logger.info('array ’ + ha.inspect)  
> ha is still the original array.

> development.log :-  
> hash {“a”=\>100, “c”=\>300, “d”=\>400}  
> array {“a”=\>100, “c”=\>300, “d”=\>400}
> 
> IRB does not show this problem

Only because you didn’t ask it the right questions:

irb(main):001:0\> ha = { “c” =\> 300, “a” =\> 100, “d” =\> 400, “c” =\> 300  
}  
=\> {“a”=\>100, “c”=\>300, “d”=\>400}  
irb(main):002:0\> ha.to\_a  
=\> [[“a”, 100], [“c”, 300], [“d”, 400]]  
irb(main):003:0\> ha  
=\> {“a”=\>100, “c”=\>300, “d”=\>400}  
irb(main):004:0\>

Since irb prints (actually it uses p) the value of each expression you  
enter, you get to see the array created by line 2, before it’s garbage  
collected. But line 3 shows that the binding of ha to the original  
hash has not changed.

try something like this:

hash = { “c” =\> 300, “a” =\> 100, “d” =\> 400, “c” =\> 300 }  
logger.info('hash ’ + hash.inspect)  
array = hash.to\_a  
logger.info('array ’ + array.inspect)

Don’t confuse variables with objects in Ruby.  
[http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-values-and-objects](http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-values-and-objects)

–  
Rick DeNatale

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