doc/classes/Binding.html in ruby-breakpoint-0.5.0 vs doc/classes/Binding.html in ruby-breakpoint-0.5.1

- old
+ new

@@ -1,221 +1,238 @@ -<?xml version="1.0" encoding="iso-8859-1"?> -<!DOCTYPE html - PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> - -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - <title>Class: Binding</title> - <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> - <meta http-equiv="Content-Script-Type" content="text/javascript" /> - <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" /> - <script type="text/javascript"> - // <![CDATA[ - - function popupCode( url ) { - window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") - } - - function toggleCode( id ) { - if ( document.getElementById ) - elem = document.getElementById( id ); - else if ( document.all ) - elem = eval( "document.all." + id ); - else - return false; - - elemStyle = elem.style; - - if ( elemStyle.display != "block" ) { - elemStyle.display = "block" - } else { - elemStyle.display = "none" - } - - return true; - } - - // Make codeblocks hidden by default - document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) - - // ]]> - </script> - -</head> -<body> - - - - <div id="classHeader"> - <table class="header-table"> - <tr class="top-aligned-row"> - <td><strong>Class</strong></td> - <td class="class-name-in-header">Binding</td> - </tr> - <tr class="top-aligned-row"> - <td><strong>In:</strong></td> - <td> - <a href="../files/lib/binding_of_caller_rb.html"> - lib/binding_of_caller.rb - </a> - <br /> - </td> - </tr> - - <tr class="top-aligned-row"> - <td><strong>Parent:</strong></td> - <td> - Object - </td> - </tr> - </table> - </div> - <!-- banner header --> - - <div id="bodyContent"> - - - - <div id="contextContent"> - - - - </div> - - <div id="method-list"> - <h3 class="section-bar">Methods</h3> - - <div class="name-list"> - <a href="#M000003">of_caller</a>&nbsp;&nbsp; - </div> - </div> - - </div> - - - <!-- if includes --> - - <div id="section"> - - - - - - - - - <!-- if method_list --> - <div id="methods"> - <h3 class="section-bar">Public Class methods</h3> - - <div id="method-M000003" class="method-detail"> - <a name="M000003"></a> - - <div class="method-heading"> - <a href="#M000003" class="method-signature"> - <span class="method-name">of_caller</span><span class="method-args">() {|result| ...}</span> - </a> - </div> - - <div class="method-description"> - <p> -This method returns the binding of the method that called your method. It -will raise an Exception when you&#8217;re not inside a method. -</p> -<p> -It&#8217;s used like this: -</p> -<pre> - def inc_counter(amount = 1) - Binding.of_caller do |binding| - # Create a lambda that will increase the variable 'counter' - # in the caller of this method when called. - inc = eval(&quot;lambda { |arg| counter += arg }&quot;, binding) - # We can refer to amount from inside this block safely. - inc.call(amount) - end - # No other statements can go here. Put them inside the block. - end - counter = 0 - 2.times { inc_counter } - counter # =&gt; 2 -</pre> -<p> -<a href="Binding.html#M000003">Binding.of_caller</a> must be the last -statement in the method. This means that you will have to put everything -you want to do after the call to <a -href="Binding.html#M000003">Binding.of_caller</a> into the block of it. -This should be no problem however, because Ruby has closures. If you -don&#8217;t do this an Exception will be raised. Because of the way that <a -href="Binding.html#M000003">Binding.of_caller</a> is implemented it has to -be done this way. -</p> - <p><a class="source-toggle" href="#" - onclick="toggleCode('M000003-source');return false;">[Source]</a></p> - <div class="method-source-code" id="M000003-source"> -<pre> - <span class="ruby-comment cmt"># File lib/binding_of_caller.rb, line 35</span> -35: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Binding</span>.<span class="ruby-identifier">of_caller</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) -36: <span class="ruby-identifier">old_critical</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">critical</span> -37: <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">critical</span> = <span class="ruby-keyword kw">true</span> -38: <span class="ruby-identifier">count</span> = <span class="ruby-value">0</span> -39: <span class="ruby-identifier">cc</span>, <span class="ruby-identifier">result</span>, <span class="ruby-identifier">error</span>, <span class="ruby-identifier">extra_data</span> = <span class="ruby-constant">Continuation</span>.<span class="ruby-identifier">create</span>(<span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>) -40: <span class="ruby-identifier">error</span>.<span class="ruby-identifier">call</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">error</span> -41: -42: <span class="ruby-identifier">tracer</span> = <span class="ruby-identifier">lambda</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-operator">*</span><span class="ruby-identifier">args</span><span class="ruby-operator">|</span> -43: <span class="ruby-identifier">type</span>, <span class="ruby-identifier">context</span>, <span class="ruby-identifier">extra_data</span> = <span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>], <span class="ruby-identifier">args</span>[<span class="ruby-value">4</span>], <span class="ruby-identifier">args</span> -44: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;return&quot;</span> -45: <span class="ruby-identifier">count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> -46: <span class="ruby-comment cmt"># First this method and then calling one will return --</span> -47: <span class="ruby-comment cmt"># the trace event of the second event gets the context</span> -48: <span class="ruby-comment cmt"># of the method which called the method that called this</span> -49: <span class="ruby-comment cmt"># method.</span> -50: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">count</span> <span class="ruby-operator">==</span> <span class="ruby-value">2</span> -51: <span class="ruby-comment cmt"># It would be nice if we could restore the trace_func</span> -52: <span class="ruby-comment cmt"># that was set before we swapped in our own one, but</span> -53: <span class="ruby-comment cmt"># this is impossible without overloading set_trace_func</span> -54: <span class="ruby-comment cmt"># in current Ruby.</span> -55: <span class="ruby-identifier">set_trace_func</span>(<span class="ruby-keyword kw">nil</span>) -56: <span class="ruby-identifier">cc</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">eval</span>(<span class="ruby-value str">&quot;binding&quot;</span>, <span class="ruby-identifier">context</span>), <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">extra_data</span>) -57: <span class="ruby-keyword kw">end</span> -58: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;line&quot;</span> <span class="ruby-keyword kw">then</span> -59: <span class="ruby-keyword kw">nil</span> -60: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;c-return&quot;</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">extra_data</span>[<span class="ruby-value">3</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">:set_trace_func</span> <span class="ruby-keyword kw">then</span> -61: <span class="ruby-keyword kw">nil</span> -62: <span class="ruby-keyword kw">else</span> -63: <span class="ruby-identifier">set_trace_func</span>(<span class="ruby-keyword kw">nil</span>) -64: <span class="ruby-identifier">error_msg</span> = <span class="ruby-value str">&quot;Binding.of_caller used in non-method context or &quot;</span> <span class="ruby-operator">+</span> -65: <span class="ruby-value str">&quot;trailing statements of method using it aren't in the block.&quot;</span> -66: <span class="ruby-identifier">cc</span>.<span class="ruby-identifier">call</span>(<span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">lambda</span> { <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ArgumentError</span>, <span class="ruby-identifier">error_msg</span>) }, <span class="ruby-keyword kw">nil</span>) -67: <span class="ruby-keyword kw">end</span> -68: <span class="ruby-keyword kw">end</span> -69: -70: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">result</span> -71: <span class="ruby-identifier">set_trace_func</span>(<span class="ruby-identifier">tracer</span>) -72: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> -73: <span class="ruby-keyword kw">else</span> -74: <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">critical</span> = <span class="ruby-identifier">old_critical</span> -75: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">arity</span> -76: <span class="ruby-keyword kw">when</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">then</span> <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">result</span>) -77: <span class="ruby-keyword kw">else</span> <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">result</span>, <span class="ruby-identifier">extra_data</span>) -78: <span class="ruby-keyword kw">end</span> -79: <span class="ruby-keyword kw">end</span> -80: <span class="ruby-keyword kw">end</span> -</pre> - </div> - </div> - </div> - - - </div> - - - </div> - - -<div id="validator-badges"> - <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> -</div> - -</body> +<?xml version="1.0" encoding="iso-8859-1"?> +<!DOCTYPE html + PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title>Class: Binding</title> + <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> + <meta http-equiv="Content-Script-Type" content="text/javascript" /> + <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" /> + <script type="text/javascript"> + // <![CDATA[ + + function popupCode( url ) { + window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") + } + + function toggleCode( id ) { + if ( document.getElementById ) + elem = document.getElementById( id ); + else if ( document.all ) + elem = eval( "document.all." + id ); + else + return false; + + elemStyle = elem.style; + + if ( elemStyle.display != "block" ) { + elemStyle.display = "block" + } else { + elemStyle.display = "none" + } + + return true; + } + + // Make codeblocks hidden by default + document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) + + // ]]> + </script> + +</head> +<body> + + + + <div id="classHeader"> + <table class="header-table"> + <tr class="top-aligned-row"> + <td><strong>Class</strong></td> + <td class="class-name-in-header">Binding</td> + </tr> + <tr class="top-aligned-row"> + <td><strong>In:</strong></td> + <td> + <a href="../files/lib/binding_of_caller_rb.html"> + lib/binding_of_caller.rb + </a> + <br /> + </td> + </tr> + + <tr class="top-aligned-row"> + <td><strong>Parent:</strong></td> + <td> + Object + </td> + </tr> + </table> + </div> + <!-- banner header --> + + <div id="bodyContent"> + + + + <div id="contextContent"> + + + + </div> + + <div id="method-list"> + <h3 class="section-bar">Methods</h3> + + <div class="name-list"> + <a href="#M000003">of_caller</a>&nbsp;&nbsp; + </div> + </div> + + </div> + + + <!-- if includes --> + + <div id="section"> + + + + + + + + + <!-- if method_list --> + <div id="methods"> + <h3 class="section-bar">Public Class methods</h3> + + <div id="method-M000003" class="method-detail"> + <a name="M000003"></a> + + <div class="method-heading"> + <a href="#M000003" class="method-signature"> + <span class="method-name">of_caller</span><span class="method-args">() {|result| ...}</span> + </a> + </div> + + <div class="method-description"> + <p> +This method returns the binding of the method that called your method. It +will raise an Exception when you&#8217;re not inside a method. +</p> +<p> +It&#8217;s used like this: +</p> +<pre> + def inc_counter(amount = 1) + Binding.of_caller do |binding| + # Create a lambda that will increase the variable 'counter' + # in the caller of this method when called. + inc = eval(&quot;lambda { |arg| counter += arg }&quot;, binding) + # We can refer to amount from inside this block safely. + inc.call(amount) + end + # No other statements can go here. Put them inside the block. + end + counter = 0 + 2.times { inc_counter } + counter # =&gt; 2 +</pre> +<p> +<a href="Binding.html#M000003">Binding.of_caller</a> must be the last +statement in the method. This means that you will have to put everything +you want to do after the call to <a +href="Binding.html#M000003">Binding.of_caller</a> into the block of it. +This should be no problem however, because Ruby has closures. If you +don&#8217;t do this an Exception will be raised. Because of the way that <a +href="Binding.html#M000003">Binding.of_caller</a> is implemented it has to +be done this way. +</p> +<p> +Please note that currently bindings returned by <a +href="Binding.html#M000003">Binding.of_caller</a>() will have a wrong self +context which means you can not call methods, access instance variables and +so on on the calling object. You can work around this by defining the +method which uses the binding on all objects and telling your users to use +them without a receiver. This is how ruby-breakpoint works around the +problem. +</p> +<p> +This is believed to be a bug in Ruby and has been reported to ruby-core. +See <a +href="http://www.ruby-forum.com/topic/67255">www.ruby-forum.com/topic/67255</a> +</p> + <p><a class="source-toggle" href="#" + onclick="toggleCode('M000003-source');return false;">[Source]</a></p> + <div class="method-source-code" id="M000003-source"> +<pre> + <span class="ruby-comment cmt"># File lib/binding_of_caller.rb, line 46</span> +46: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Binding</span>.<span class="ruby-identifier">of_caller</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) +47: <span class="ruby-identifier">old_critical</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">critical</span> +48: <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">critical</span> = <span class="ruby-keyword kw">true</span> +49: <span class="ruby-identifier">count</span> = <span class="ruby-value">0</span> +50: <span class="ruby-identifier">cc</span>, <span class="ruby-identifier">result</span>, <span class="ruby-identifier">error</span>, <span class="ruby-identifier">extra_data</span> = <span class="ruby-constant">Continuation</span>.<span class="ruby-identifier">create</span>(<span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>) +51: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">error</span> <span class="ruby-keyword kw">then</span> +52: <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">critical</span> = <span class="ruby-identifier">old_critical</span> +53: <span class="ruby-identifier">error</span>.<span class="ruby-identifier">call</span> +54: <span class="ruby-keyword kw">end</span> +55: +56: <span class="ruby-identifier">tracer</span> = <span class="ruby-identifier">lambda</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-operator">*</span><span class="ruby-identifier">args</span><span class="ruby-operator">|</span> +57: <span class="ruby-identifier">type</span>, <span class="ruby-identifier">context</span>, <span class="ruby-identifier">extra_data</span> = <span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>], <span class="ruby-identifier">args</span>[<span class="ruby-value">4</span>], <span class="ruby-identifier">args</span> +58: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;return&quot;</span> +59: <span class="ruby-identifier">count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> +60: <span class="ruby-comment cmt"># First this method and then calling one will return -- </span> +61: <span class="ruby-comment cmt"># the trace event of the second event gets the context </span> +62: <span class="ruby-comment cmt"># of the method which called the method that called this </span> +63: <span class="ruby-comment cmt"># method. </span> +64: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">count</span> <span class="ruby-operator">==</span> <span class="ruby-value">2</span> +65: <span class="ruby-comment cmt"># It would be nice if we could restore the trace_func </span> +66: <span class="ruby-comment cmt"># that was set before we swapped in our own one, but </span> +67: <span class="ruby-comment cmt"># this is impossible without overloading set_trace_func </span> +68: <span class="ruby-comment cmt"># in current Ruby. </span> +69: <span class="ruby-identifier">set_trace_func</span>(<span class="ruby-keyword kw">nil</span>) +70: <span class="ruby-identifier">cc</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">context</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">extra_data</span>) +71: <span class="ruby-keyword kw">end</span> +72: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;line&quot;</span> <span class="ruby-keyword kw">then</span> +73: <span class="ruby-keyword kw">nil</span> +74: <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;c-return&quot;</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">extra_data</span>[<span class="ruby-value">3</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">:set_trace_func</span> <span class="ruby-keyword kw">then</span> +75: <span class="ruby-keyword kw">nil</span> +76: <span class="ruby-keyword kw">else</span> +77: <span class="ruby-identifier">set_trace_func</span>(<span class="ruby-keyword kw">nil</span>) +78: <span class="ruby-identifier">error_msg</span> = <span class="ruby-value str">&quot;Binding.of_caller used in non-method context or &quot;</span> <span class="ruby-operator">+</span> +79: <span class="ruby-value str">&quot;trailing statements of method using it aren't in the block.&quot;</span> +80: <span class="ruby-identifier">cc</span>.<span class="ruby-identifier">call</span>(<span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">lambda</span> { <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ArgumentError</span>, <span class="ruby-identifier">error_msg</span>) }, <span class="ruby-keyword kw">nil</span>) +81: <span class="ruby-keyword kw">end</span> +82: <span class="ruby-keyword kw">end</span> +83: +84: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">result</span> +85: <span class="ruby-identifier">set_trace_func</span>(<span class="ruby-identifier">tracer</span>) +86: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> +87: <span class="ruby-keyword kw">else</span> +88: <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">critical</span> = <span class="ruby-identifier">old_critical</span> +89: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">arity</span> +90: <span class="ruby-keyword kw">when</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">then</span> <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">result</span>) +91: <span class="ruby-keyword kw">else</span> <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">result</span>, <span class="ruby-identifier">extra_data</span>) +92: <span class="ruby-keyword kw">end</span> +93: <span class="ruby-keyword kw">end</span> +94: <span class="ruby-keyword kw">end</span> +</pre> + </div> + </div> + </div> + + + </div> + + + </div> + + +<div id="validator-badges"> + <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> +</div> + +</body> </html> \ No newline at end of file