Find the ObjectID for an array?
-
I am working with some arrays and I was wondering how to find the objectID of each array? I am imagining doing something like:
puts my_array.object_id
Anything like that possible? I'm probably just overlooking the obvious...
Chris
-
Oh! I found it. my_array.id gives an object id. That works. I was having problems with duplicate arrays and I was able to track them down with this. This is what the problem was.
I was creating an array full of empty arrays with this:
needed_arrays = 100 my_array = Array.new(needed_arrays, Array.new)
Apparently that makes an array with 100 identical arrays. So if you push something to my_array[0] or my_arra[99], it is all going into the same ayyar apparently....
Instead I used
my_array = [] needed_arrays = 100 needed_arrays.times { my_array << [] }
and that is working much better.
Chris
-
Actually, the method is .object_id
@chris fullmer said:
Apparently that makes an array with 100 identical arrays.
100 references to the same Array, but not 100 arrays. I know, I'm being pedantic.
Create them as needed:
my_array = [] my_array[index] ||= [] my_array[index].push(value)
-
@jim said:
100 references to the same Array, but not 100 arrays. I know, I'm being pedantic.
Its ok, its good for me. And is there a difference between .object_id and .id and .id ? And I saw a hint at
:name
- what is that? I couldn't get it to work.And in your example, what does ||= do? I'm guessing it creates an empty array at [index] if there is not an array there already?
Thanks Jim.
Chris
Advertisement