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
|
<ppdoc>
<copyright>
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/).
</copyright>
<class name="Binding" super="Object" type="class"> Objects of class <classname>Binding</classname> encapsulate the execution context at some
particular place in the code and retain this context for future use.
The variables, methods, value of <tt>self</tt>, and possibly an iterator
block that can be accessed in this context are all retained.
Binding objects can be created using <mim><file>kernel</file><front>Kernel</front><back>binding</back><mref>binding</mref></mim>, and are
made available to the callback of <mim><file>kernel</file><front>Kernel</front><back>set_trace_func</back><mref>set_trace_func</mref></mim>.
<p/>
These binding objects can be passed as the second argument of the
<mim><file>kernel</file><front>Kernel</front><back>eval</back><mref>eval</mref></mim> method, establishing an environment for the
evaluation.
<p/>
<codefragment>
<fullcode><![CDATA[ class Demo
def initialize(n)
@secret = n
end
def getBinding
return binding()
end
end
k1 = Demo.new(99)
b1 = k1.getBinding
k2 = Demo.new(-3)
b2 = k2.getBinding
eval("@secret", b1)
eval("@secret", b2)
eval("@secret")
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>class<nbsp/>Demo</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>initialize(n)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>@secret<nbsp/>=<nbsp/>n</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>getBinding</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>return<nbsp/>binding()</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td colspan="3"><tt>k1<nbsp/>=<nbsp/>Demo.new(99)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>b1<nbsp/>=<nbsp/>k1.getBinding</tt></td>
</tr>
<tr>
<td colspan="3"><tt>k2<nbsp/>=<nbsp/>Demo.new(-3)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>b2<nbsp/>=<nbsp/>k2.getBinding</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td><tt>eval("@secret",<nbsp/>b1)</tt></td>
<td>»</td>
<td><tt>99</tt></td>
</tr>
<tr>
<td><tt>eval("@secret",<nbsp/>b2)</tt></td>
<td>»</td>
<td><tt>-3</tt></td>
</tr>
<tr>
<td><tt>eval("@secret")</tt></td>
<td>»</td>
<td><tt>nil</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
Binding objects have no class-specific methods.
</class>
</ppdoc>
|