标题说明了一切.也许有一种方法可以被称为def destruct; delete self;end?
这是可能的,但绝对不推荐,我将向您展示的方式可能在未来改变或打破.你为什么需要这个?GC的想法正是不用担心这样的事情.
class Foo
def initialize
@x = 10
end
def finalize
puts "Never called"
end
end
foo = Foo.new
p foo # => #<Foo:0x10be27fd0 @x=10>
GC.free(Pointer(Void).new(foo.object_id)) # This line frees the memory
p foo # => #<Foo:0x10be27fd0 @x=1>
Run Code Online (Sandbox Code Playgroud)