我正试图找到一种方法来安全地释放课堂上获得的资源。我尝试使用finalize
,但它不可靠。有时我会在GC有机会释放资源之前关闭我的程序。
所以我决定在这样的块中使用类实例:
class Foo
def destroy # free resources
#...
end
#...
def self.create(*args)
instance = self.new(*args)
begin
yield instance
ensure
instance.destroy
end
end
Foo.create do |foo|
# use foo
end
工作正常,但我仍然可以使用new
创建一个我必须destroy
显式的实例。我尝试编写自己的new
,但似乎默认情况下它只是超载new
。
有没有办法重新定义\ disable new
?
答案 0 :(得分:4)
这是private
方法,应该class Foo
@foo : String
private def initialize(@foo)
end
def destroy
puts "Destroying #{self}"
end
def self.create(arg)
instance = new(arg)
yield instance
ensure
instance.destroy if instance
end
end
Foo.create("bar") do |foo| # will work
p foo
end
Foo.new("bar") # will raise
:
<script type="text/javascript">
function showHint(str){
if(str.length==0){
document.getElementById("ddlstate").innerHTML="";
return;
}
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById("ddlstate").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET","getstate.php?q="+str,true);
xmlHttp.send();
}
</script>
<script type="text/javascript">
function showHide(str){
if(str.length==0){
document.getElementById("ddldis").innerHTML="";
return;
}
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById("ddldis").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET","getdis.php?q="+str,true);
xmlHttp.send();
}
</script>
<form method="post">
<div class="col-lg-3 form-group">
<center>
<img src="images/placeholder-2.png"/ width="50px;"><br>
<label>Location</label>
</center>
<select id="ddlcnt" onchange="showHint(this.value)" class="form-control">
<option value="0">-----Select Location-----</option>
<?php
error_reporting(0);
$con=mysql_connect("localhost","root","");
mysql_select_db("doctor_db",$con);
$sql="select * from location_tb";
$res=mysql_query($sql);
while($ar=mysql_fetch_array($res)){
?>
<option value="<?php echo $ar[0];?>"><?php echo $ar[1];?></option>
<?php } ?>
</select>
</div>
<div class="col-lg-3 form-group">
<center>
<img src="images/surgeon.png"/ width="50px;">
<!--<img src="images/stethoscope.png"/ width="50px;">--><br>
<!-- <label>Doctors</label>-->
<label>Specialty</label>
</center>
<select id="ddlstate" name="ddlstate" class="form-control">
</select>
</div>
<div class="col-lg-4 form-group">
<div class="col-lg-10 mrg_less">
<center>
<img src="images/stethoscope.png"/ width="50px;"><br>
<label>Doctors</label>
</center>
</div>
<div class="col-lg-2"></div>
<div class="col-lg-9 mrg_less">
<select id="ddldis" name="ddldis" class="form-control"></select>
</div>
<div class="col-lg-2">
<button class="search" name="search" id="search" type="submit"><i class="flaticon-magnifier-tool"></i></button>