I'm working with a large image array, so i've packed it. To access the pixels in the array i've implemented two methods.
def get_p(a)
data=a.unpack('S9s')
end
def put_p(array,index_a,value)
index=index_a[0]
k=array.unpack('S9s')
k[index]=value
k.pack('S9s')
end
It works, but i wondered if there was a more elegant way to do this. Makes my code look different than my standard array functions.
If get_p(image_data[i][j+1])[BLOB]==0
vs
if image_data[i][j+1][BLOB]==0
Also, don't know if anyone cares, but unpack doesn't seem to be documented anywhere, i was lucky to find a reference here, but it took some time.
You could craete a class like:
class PackedArray
def initialize(array)
@packed_array = array.pack('S9s')
end
def [](key)
data = @packed_array.unpack('S9s')
data[key]
end
def []=(key, val)
k = @packed_array.unpack('S9s')
k[key]=val
@packed_array = k.pack('S9s')
end
end
Then, fill your image_data[i][j]
with an instance of this class. E.g.
for i in [0..image_data.size]
for j in [0..image_data[i].size]
image_data[i][j] = new PackedArray(image_data[i][j])
end
end
And finally you can simply use:
if image_data[i][j+1][BLOB] == 0
Without needing of packing/unpacking manually.