标题已经说明了一切。也许有一种方法可以像
def destruct; delete self;end
这样调用?def destruct; delete self;end
这样调用?这是可能的,但绝对不建议这样做。我将向您展示的方法可能会在未来发生变化或出现故障。为什么需要这样做呢?垃圾回收的理念正是不必担心这些问题。
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>