# Local variables with hashes

**URL:** <https://www.ruby-forum.com/t/local-variables-with-hashes/154190>\
**Category:** Rails\
**Created:** [November 26, 2008, 10:14am UTC](https://www.ruby-forum.com/t/local-variables-with-hashes/154190 "2008-11-26T10:14:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lazah](https://www.ruby-forum.com/u/lazah)\
**Post date:** [November 26, 2008, 10:14am UTC](https://www.ruby-forum.com/t/local-variables-with-hashes/154190/1 "2008-11-26T10:14:04Z")

</div>

I have a question about using local variables as values for ruby hashes.  
I have to do a script that will gather meta data of video files in a  
fixed directory and store them in an array of hashes. The problem is  
that when I have collected the meta data to a hash called video and  
append this to the end of videos array all the hash entries are the same  
in the array. I assign the values to the hash and append to the videos  
array with this script block:

video[“res\_w”] = res\_w  
video[“res\_h”] = res\_h  
video[“dur\_h”] = dur\_h  
video[“dur\_m”] = dur\_m  
video[“dur\_s”] = dur\_s  
video[“dur\_ms”] = dur\_ms  
videos \<\< video

All the local variables have been initialized as zeroes and when my loop  
retrieves another file for processing it reuses the local variables. The  
actual question is does the hash store a pointer to the local variable  
as its value? If it does is there a way to overwrite this behaviour or  
some way around it?

---

<div class="post-metadata">

**Author:** ![lazah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/958977/32.png) [@lazah](https://www.ruby-forum.com/u/lazah)\
**Post date:** [November 29, 2008, 10:52am UTC](https://www.ruby-forum.com/t/local-variables-with-hashes/154190/2 "2008-11-29T10:52:20Z")

</div>

Pentti Laitinen wrote:

> video[“dur\_h”] = dur\_h  
> video[“dur\_m”] = dur\_m  
> video[“dur\_s”] = dur\_s  
> video[“dur\_ms”] = dur\_ms  
> videos \<\< video
> 
> All the local variables have been initialized as zeroes and when my loop  
> retrieves another file for processing it reuses the local variables. The  
> actual question is does the hash store a pointer to the local variable  
> as its value? If it does is there a way to overwrite this behaviour or  
> some way around it?

Integer hash values aren’t stored as object references.

Perhaps your problem is that you’re not re-initializing  
video to {} each time around the loop, so you’re always  
updating a common video object.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
