I am learning ruby and haven't found a way to override an equivalent object.delete
function:
This is how I am doing it:
class Foo
@@no_foo=0
def initialize
@@no_foo+=1
end
def delete
#class specific cleanup...
@@no_foo-=1
end
def Foo.no_foo
return "#@@no_foo"
end
end
def delete(obj)
#object independent cleanup...
obj.delete
return nil
end
foo1 = Foo.new
foo2 = Foo.new
puts Foo.no_foo
puts foo2
foo2 = delete(foo2)
puts foo2
puts Foo.no_foo
As you can see, this is a bit of a hacky way of going about things...
is there an easier way to go about this?
Basically I would like to make my objects unaccessible in the same call as decrementing the total counter for that class.
I couldn't find a method that gets called when setting a variable(pointer to object) to nil.
I found no way to delete an object.
WeakRef
. – Daguerre