【发布时间】:2012-11-01 00:42:50
【问题描述】:
可能重复:
Notification of object destruction in Ruby
Ruby: Destructors?
#initialize 在类被实例化时启动函数。只是想知道是否有 #uninitialize 函数。例如:
class Something
def initialize
@browser = Watir::Browser.new :chrome #opens browser
end
def stuff(url)
@browser.goto url
end
def uninitalize
@browser.quit #close browser
end
end
s = Something.new
s.stuff("google.ca")
在这种情况下,浏览器将被初始化函数打开。有没有办法自动退出?
【问题讨论】:
-
请参阅stackoverflow.com/questions/5956067/ruby-destructors,了解如何在 Ruby 中使用析构函数。
-
如果您希望对象在内部处理所有内容而不是响应消息,为什么不将
@browser.stuff(url); @browser.quit直接放入#initialize?
标签: ruby destructor instantiation