Pentti Laitinen wrote:> 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?
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---