I have the following code in one of my personal projects:
def allocate(var, value) # Allocate the variable to the next available spot.
@storage.each do |mem_loc|
if mem_loc.free?
mem_loc.set(var, value) # Set it then break out of the loop.
break
end
end
end
Each item in the storage array is an object that responds to free? and set. What I am trying to do is cycle through the array, looking for the next free (empty) object to set the variable to. My problem is, this just cycles through every object and sets them all. Am I using the break function incorrectly?
Testing it, I call the following:
store.allocate(:a, 10)
store.allocate(:b, 20)
So store[1] should be set to :b and 20. But when I output the contents, it's value is 10, as is the rest of the array.