HEX
Server: Apache
System: Windows NT MAGNETO-ARM 10.0 build 22000 (Windows 10) AMD64
User: Michel (0)
PHP: 7.4.7
Disabled: NONE
Upload Files
File: C:/Ruby27-x64/share/doc/ruby/html/Binding.html
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>class Binding - RDoc Documentation</title>

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
</script>

<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">




<body id="top" role="document" class="class">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="./index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  

  <div id="class-metadata">
    
    <div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  
  <p class="link"><a href="Object.html">Object</a>
  
</div>

    
    
    <!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    
    <li ><a href="#method-i-eval">#eval</a>
    
    <li ><a href="#method-i-irb">#irb</a>
    
    <li ><a href="#method-i-local_variable_defined-3F">#local_variable_defined?</a>
    
    <li ><a href="#method-i-local_variable_get">#local_variable_get</a>
    
    <li ><a href="#method-i-local_variable_set">#local_variable_set</a>
    
    <li ><a href="#method-i-local_variables">#local_variables</a>
    
    <li ><a href="#method-i-receiver">#receiver</a>
    
    <li ><a href="#method-i-source_location">#source_location</a>
    
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-labelledby="class-Binding">
  <h1 id="class-Binding" class="class">
    class Binding
  </h1>

  <section class="description">
    
<p>Objects of class <a href="Binding.html"><code>Binding</code></a> encapsulate the execution context at some particular place in the code and retain this context for future use. The variables, methods, value of <code>self</code>, and possibly an iterator block that can be accessed in this context are all retained. <a href="Binding.html"><code>Binding</code></a> objects can be created using <a href="Kernel.html#method-i-binding"><code>Kernel#binding</code></a>, and are made available to the callback of <a href="Kernel.html#method-i-set_trace_func"><code>Kernel#set_trace_func</code></a> and instances of <a href="TracePoint.html"><code>TracePoint</code></a>.</p>

<p>These binding objects can be passed as the second argument of the <a href="Kernel.html#method-i-eval"><code>Kernel#eval</code></a> method, establishing an environment for the evaluation.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Demo</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">n</span>)
    <span class="ruby-ivar">@secret</span> = <span class="ruby-identifier">n</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_binding</span>
    <span class="ruby-identifier">binding</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">k1</span> = <span class="ruby-constant">Demo</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">99</span>)
<span class="ruby-identifier">b1</span> = <span class="ruby-identifier">k1</span>.<span class="ruby-identifier">get_binding</span>
<span class="ruby-identifier">k2</span> = <span class="ruby-constant">Demo</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">-3</span>)
<span class="ruby-identifier">b2</span> = <span class="ruby-identifier">k2</span>.<span class="ruby-identifier">get_binding</span>

<span class="ruby-identifier">eval</span>(<span class="ruby-string">&quot;@secret&quot;</span>, <span class="ruby-identifier">b1</span>)   <span class="ruby-comment">#=&gt; 99</span>
<span class="ruby-identifier">eval</span>(<span class="ruby-string">&quot;@secret&quot;</span>, <span class="ruby-identifier">b2</span>)   <span class="ruby-comment">#=&gt; -3</span>
<span class="ruby-identifier">eval</span>(<span class="ruby-string">&quot;@secret&quot;</span>)       <span class="ruby-comment">#=&gt; nil</span>
</pre>

<p><a href="Binding.html"><code>Binding</code></a> objects have no class-specific methods.</p>

  </section>

  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    

    

    
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
       <header>
         <h3>Public Instance Methods</h3>
       </header>

    
      <div id="method-i-eval" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            eval(string [, filename [,lineno]])  &rarr; obj
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Evaluates the Ruby expression(s) in <em>string</em>, in the <em>binding</em>&#39;s context.  If the optional <em>filename</em> and <em>lineno</em> parameters are present, they will be used when reporting syntax errors.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">get_binding</span>(<span class="ruby-identifier">param</span>)
  <span class="ruby-identifier">binding</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">b</span> = <span class="ruby-identifier">get_binding</span>(<span class="ruby-string">&quot;hello&quot;</span>)
<span class="ruby-identifier">b</span>.<span class="ruby-identifier">eval</span>(<span class="ruby-string">&quot;param&quot;</span>)   <span class="ruby-comment">#=&gt; &quot;hello&quot;</span>
</pre>
          
          

          
          <div class="method-source-code" id="eval-source">
            <pre>static VALUE
bind_eval(int argc, VALUE *argv, VALUE bindval)
{
    VALUE args[4];

    rb_scan_args(argc, argv, &quot;12&quot;, &amp;args[0], &amp;args[2], &amp;args[3]);
    args[1] = bindval;
    return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-irb" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">irb</span><span
            class="method-args">()</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Opens an IRB session where <code>binding.irb</code> is called which allows for interactive debugging. You can call any methods or variables available in the current scope, and mutate state if you need to.</p>

<p>Given a Ruby file called <code>potato.rb</code> containing the following code:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Potato</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>
    <span class="ruby-ivar">@cooked</span> = <span class="ruby-keyword">false</span>
    <span class="ruby-identifier">binding</span>.<span class="ruby-identifier">irb</span>
    <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;Cooked potato: #{@cooked}&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Potato</span>.<span class="ruby-identifier">new</span>
</pre>

<p>Running <code>ruby potato.rb</code> will open an IRB session where <code>binding.irb</code> is called, and you will see the following:</p>

<pre>$ ruby potato.rb

From: potato.rb @ line 4 :

    1: class Potato
    2:   def initialize
    3:     @cooked = false
 =&gt; 4:     binding.irb
    5:     puts &quot;Cooked potato: #{@cooked}&quot;
    6:   end
    7: end
    8:
    9: Potato.new

irb(#&lt;Potato:0x00007feea1916670&gt;):001:0&gt;</pre>

<p>You can type any valid Ruby code and it will be evaluated in the current context. This allows you to debug without having to run your code repeatedly:</p>

<pre>irb(#&lt;Potato:0x00007feea1916670&gt;):001:0&gt; @cooked
=&gt; false
irb(#&lt;Potato:0x00007feea1916670&gt;):002:0&gt; self.class
=&gt; Potato
irb(#&lt;Potato:0x00007feea1916670&gt;):003:0&gt; caller.first
=&gt; &quot;.../2.5.1/lib/ruby/2.5.0/irb/workspace.rb:85:in `eval&#39;&quot;
irb(#&lt;Potato:0x00007feea1916670&gt;):004:0&gt; @cooked = true
=&gt; true</pre>

<p>You can exit the IRB session with the <code>exit</code> command. Note that exiting will resume execution where <code>binding.irb</code> had paused it, as you can see from the output printed to standard output in this example:</p>

<pre>irb(#&lt;Potato:0x00007feea1916670&gt;):005:0&gt; exit
Cooked potato: true</pre>

<p>See IRB for more information.</p>
          
          

          
          <div class="method-source-code" id="irb-source">
            <pre><span class="ruby-comment"># File lib/irb.rb, line 901</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">irb</span>
  <span class="ruby-constant">IRB</span>.<span class="ruby-identifier">setup</span>(<span class="ruby-identifier">source_location</span>[<span class="ruby-value">0</span>], <span class="ruby-value">argv:</span> [])
  <span class="ruby-identifier">workspace</span> = <span class="ruby-constant">IRB</span><span class="ruby-operator">::</span><span class="ruby-constant">WorkSpace</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">self</span>)
  <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">print</span>(<span class="ruby-identifier">workspace</span>.<span class="ruby-identifier">code_around_binding</span>)
  <span class="ruby-identifier">binding_irb</span> = <span class="ruby-constant">IRB</span><span class="ruby-operator">::</span><span class="ruby-constant">Irb</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">workspace</span>)
  <span class="ruby-identifier">binding_irb</span>.<span class="ruby-identifier">context</span>.<span class="ruby-identifier">irb_path</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-identifier">source_location</span>[<span class="ruby-value">0</span>])
  <span class="ruby-identifier">binding_irb</span>.<span class="ruby-identifier">run</span>(<span class="ruby-constant">IRB</span>.<span class="ruby-identifier">conf</span>)
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-local_variable_defined-3F" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            local_variable_defined?(symbol) &rarr; obj
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns <code>true</code> if a local variable <code>symbol</code> exists.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
  <span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>
  <span class="ruby-identifier">binding</span>.<span class="ruby-identifier">local_variable_defined?</span>(<span class="ruby-value">:a</span>) <span class="ruby-comment">#=&gt; true</span>
  <span class="ruby-identifier">binding</span>.<span class="ruby-identifier">local_variable_defined?</span>(<span class="ruby-value">:b</span>) <span class="ruby-comment">#=&gt; false</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This method is the short version of the following code:</p>

<pre class="ruby"><span class="ruby-identifier">binding</span>.<span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;defined?(#{symbol}) == &#39;local-variable&#39;&quot;</span>)
</pre>
          
          

          
          <div class="method-source-code" id="local_variable_defined-3F-source">
            <pre>static VALUE
bind_local_variable_defined_p(VALUE bindval, VALUE sym)
{
    ID lid = check_local_id(bindval, &amp;sym);
    const rb_binding_t *bind;
    const rb_env_t *env;

    if (!lid) return Qfalse;

    GetBindingPtr(bindval, bind);
    env = VM_ENV_ENVVAL_PTR(vm_block_ep(&amp;bind-&gt;block));
    return get_local_variable_ptr(&amp;env, lid) ? Qtrue : Qfalse;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-local_variable_get" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            local_variable_get(symbol) &rarr; obj
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the value of the local variable <code>symbol</code>.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
  <span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>
  <span class="ruby-identifier">binding</span>.<span class="ruby-identifier">local_variable_get</span>(<span class="ruby-value">:a</span>) <span class="ruby-comment">#=&gt; 1</span>
  <span class="ruby-identifier">binding</span>.<span class="ruby-identifier">local_variable_get</span>(<span class="ruby-value">:b</span>) <span class="ruby-comment">#=&gt; NameError</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This method is the short version of the following code:</p>

<pre class="ruby"><span class="ruby-identifier">binding</span>.<span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;#{symbol}&quot;</span>)
</pre>
          
          

          
          <div class="method-source-code" id="local_variable_get-source">
            <pre>static VALUE
bind_local_variable_get(VALUE bindval, VALUE sym)
{
    ID lid = check_local_id(bindval, &amp;sym);
    const rb_binding_t *bind;
    const VALUE *ptr;
    const rb_env_t *env;

    if (!lid) goto undefined;

    GetBindingPtr(bindval, bind);

    env = VM_ENV_ENVVAL_PTR(vm_block_ep(&amp;bind-&gt;block));
    if ((ptr = get_local_variable_ptr(&amp;env, lid)) == NULL) {
        sym = ID2SYM(lid);
      undefined:
        rb_name_err_raise(&quot;local variable `%1$s&#39; is not defined for %2$s&quot;,
                          bindval, sym);
    }

    return *ptr;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-local_variable_set" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            local_variable_set(symbol, obj) &rarr; obj
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p><a href="Set.html"><code>Set</code></a> local variable named <code>symbol</code> as <code>obj</code>.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
  <span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>
  <span class="ruby-identifier">bind</span> = <span class="ruby-identifier">binding</span>
  <span class="ruby-identifier">bind</span>.<span class="ruby-identifier">local_variable_set</span>(<span class="ruby-value">:a</span>, <span class="ruby-value">2</span>) <span class="ruby-comment"># set existing local variable `a&#39;</span>
  <span class="ruby-identifier">bind</span>.<span class="ruby-identifier">local_variable_set</span>(<span class="ruby-value">:b</span>, <span class="ruby-value">3</span>) <span class="ruby-comment"># create new local variable `b&#39;</span>
                                 <span class="ruby-comment"># `b&#39; exists only in binding</span>

  <span class="ruby-identifier">p</span> <span class="ruby-identifier">bind</span>.<span class="ruby-identifier">local_variable_get</span>(<span class="ruby-value">:a</span>)  <span class="ruby-comment">#=&gt; 2</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">bind</span>.<span class="ruby-identifier">local_variable_get</span>(<span class="ruby-value">:b</span>)  <span class="ruby-comment">#=&gt; 3</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>                            <span class="ruby-comment">#=&gt; 2</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">b</span>                            <span class="ruby-comment">#=&gt; NameError</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This method behaves similarly to the following code:</p>

<pre class="ruby"><span class="ruby-identifier">binding</span>.<span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;#{symbol} = #{obj}&quot;</span>)
</pre>

<p>if <code>obj</code> can be dumped in Ruby code.</p>
          
          

          
          <div class="method-source-code" id="local_variable_set-source">
            <pre>static VALUE
bind_local_variable_set(VALUE bindval, VALUE sym, VALUE val)
{
    ID lid = check_local_id(bindval, &amp;sym);
    rb_binding_t *bind;
    const VALUE *ptr;
    const rb_env_t *env;

    if (!lid) lid = rb_intern_str(sym);

    GetBindingPtr(bindval, bind);
    env = VM_ENV_ENVVAL_PTR(vm_block_ep(&amp;bind-&gt;block));
    if ((ptr = get_local_variable_ptr(&amp;env, lid)) == NULL) {
        /* not found. create new env */
        ptr = rb_binding_add_dynavars(bindval, bind, 1, &amp;lid);
        env = VM_ENV_ENVVAL_PTR(vm_block_ep(&amp;bind-&gt;block));
    }

    RB_OBJ_WRITE(env, ptr, val);

    return val;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-local_variables" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            local_variables &rarr; Array
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the names of the binding&#39;s local variables as symbols.</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
  <span class="ruby-identifier">a</span> = <span class="ruby-value">1</span>
  <span class="ruby-value">2</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">n</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">binding</span>.<span class="ruby-identifier">local_variables</span> <span class="ruby-comment">#=&gt; [:a, :n]</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This method is the short version of the following code:</p>

<pre class="ruby"><span class="ruby-identifier">binding</span>.<span class="ruby-identifier">eval</span>(<span class="ruby-string">&quot;local_variables&quot;</span>)
</pre>
          
          

          
          <div class="method-source-code" id="local_variables-source">
            <pre>static VALUE
bind_local_variables(VALUE bindval)
{
    const rb_binding_t *bind;
    const rb_env_t *env;

    GetBindingPtr(bindval, bind);
    env = VM_ENV_ENVVAL_PTR(vm_block_ep(&amp;bind-&gt;block));
    return rb_vm_env_local_variables(env);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-receiver" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            receiver    &rarr; object
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the bound receiver of the binding object.</p>
          
          

          
          <div class="method-source-code" id="receiver-source">
            <pre>static VALUE
bind_receiver(VALUE bindval)
{
    const rb_binding_t *bind;
    GetBindingPtr(bindval, bind);
    return vm_block_self(&amp;bind-&gt;block);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-source_location" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            source_location  &rarr; [String, Integer]
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the Ruby source filename and line number of the binding object.</p>
          
          

          
          <div class="method-source-code" id="source_location-source">
            <pre>static VALUE
bind_location(VALUE bindval)
{
    VALUE loc[2];
    const rb_binding_t *bind;
    GetBindingPtr(bindval, bind);
    loc[0] = pathobj_path(bind-&gt;pathobj);
    loc[1] = INT2FIX(bind-&gt;first_lineno);

    return rb_ary_new4(2, loc);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
    </section>
  
  </section>

</main>


<footer id="validator-badges" role="contentinfo">
  <p><a href="https://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>