I am currently studying this piece of code. It’s a linked list implemented in Ruby.
I am particularly interested in these two method.
def removeLast
if @size <= 0
raise "No objects in list"
end
node = @last.prev
node.prev.next = @last
@last.prev = node.prev
@size -= 1
return node.object
end
def removeFirst
if @size <= 0
raise "No objects in list"
end
node = @first.next
node.next.prev = @first
@first.next = node.next
@size -= 1
return node.object
end
These two methods remove and return a node from the list. I am not sure how Ruby handles garbage collection. You will notice that both methods do not explicitly destroy the node they are trying to remove.
Is Ruby smart enough to free up this remove node from the memory without explicitly telling it to do so?
If it is not sufficient, how do I properly destroy the removed node and free up the memory?
When the garbage collector run, it will see that
nodeis no more referenced from the objects in your application and it will be deallocated.You won’t need to manually destroy it.