1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="ref_m_math.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_m_process.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
-->
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">module ObjectSpace</font></td></tr></table><p></p><H3>Index:</H3><a href="#_id2ref">_id2ref</a> <a href="#define_finalizer">define_finalizer</a> <a href="#each_object">each_object</a> <a href="#garbage_collect">garbage_collect</a> <a href="#undefine_finalizer">undefine_finalizer</a> <p></p><hr>
<P></P>
The <code>ObjectSpace</code> module contains a number of routines that
interact with the garbage collection facility and allow you to
traverse all living objects with an iterator.
<P></P>
<code>ObjectSpace</code> also provides support for object
finalizers, procs that will be called when a specific object is
about to be destroyed by garbage collection.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
include ObjectSpace
<P></P>
a = "A"
b = "B"
c = "C"
<P></P>
define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
define_finalizer(a, proc {|id| puts "Finalizer two on #{id}" })
define_finalizer(b, proc {|id| puts "Finalizer three on #{id}" })
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
0x4018d4f0
n finals=>1
Finalizer three on 537684600
0x4018d504
n finals=>0
Finalizer one on 537684610
n finals=>0
Finalizer two on 537684610
</pre></td></tr></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_id2ref"><b>_id2ref</b></a></font></td><td bgcolor="#ffaaaa">
ObjectSpace._id2ref( <i>anId</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Converts an object id to a reference to the object.
May not be
called on an object id passed as a parameter to a finalizer.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>s = "I am a string"</code></td>
<td valign="top"></td>
<td valign="top"><code>"I am a string"</code></td>
</tr>
<tr>
<td valign="top"><code>r = ObjectSpace._id2ref(s.id)</code></td>
<td valign="top"></td>
<td valign="top"><code>"I am a string"</code></td>
</tr>
<tr>
<td valign="top"><code>r == s</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="define_finalizer"><b>define_finalizer</b></a></font></td><td bgcolor="#ffaaaa">
ObjectSpace.define_finalizer(
<i>anObject</i>, <i>aProc</i>=proc()
)
</td></tr><td></td><td>
<P></P>
Adds <i>aProc</i> as a finalizer, to be called when <i>anObject</i>
is about to be destroyed.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="each_object"><b>each_object</b></a></font></td><td bgcolor="#ffaaaa">
ObjectSpace.each_object( <i>[</i>
<i>aClassOrMod</i><i>]</i> )
{| anObj | block }
-> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Calls the block once for each living, nonimmediate
object in this Ruby process.
If <i>aClassOrMod</i> is specified, calls the block for only those
classes or modules that match (or are a subclass of)
<i>aClassOrMod</i>.
Returns the number of objects found.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = 102.7
b = 95
ObjectSpace.each_object(Numeric) {|x| p x }
print "Total count: ", ObjectSpace.each_object {} ,"\n"
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
102.7
2.718281828
3.141592654
Total count: 376
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="garbage_collect"><b>garbage_collect</b></a></font></td><td bgcolor="#ffaaaa">
ObjectSpace.garbage_collect
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Initiates garbage collection (see module <code>GC</code> on page 414).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="undefine_finalizer"><b>undefine_finalizer</b></a></font></td><td bgcolor="#ffaaaa">
ObjectSpace.undefine_finalizer(
<i>anObject</i> )
</td></tr><td></td><td>
<P></P>
Removes all finalizers for <i>anObject</i>.
<P></P>
</td></table>
<P></P>
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="ref_m_math.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_m_process.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"</font><br><font size="-3">
Copyright
©
2000 Addison Wesley Longman, Inc. Released under the terms of the
<a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
<br>
This reference is available for
<a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
</font></body></html>
|