Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Don't you use <code>class_eval</code> to define a class method and <code>instance_eval</code> when you want to define an instance method?</p> </blockquote> <p>Unfortunately, it is not as straightforward as that.</p> <p>First take a closer look at what the examples of <code>class_eval</code> are doing. <code>class_eval</code> is a method which comes from Ruby's <a href="http://www.ruby-doc.org/core/classes/Module.html#M001650" rel="nofollow noreferrer">module class</a> so can be called on any class or module. When you use <code>String.class_eval</code> you are evaluating the given code in the context of the class. i.e. when you write <code>String.class_eval("def len; size; end")</code> it's exactly like you reopened the class and typed the code passed to <code>class_eval</code> e.g.</p> <pre><code>class String def len size end end </code></pre> <p>Thus to add a class method using class_eval you would write <code>String.class_eval("def self.empty; ''; end")</code> which has the same effect as:</p> <pre><code>class String def self.empty '' end end </code></pre> <p><code>instance_eval</code> is defined in Ruby's <a href="http://www.ruby-doc.org/core/classes/Object.html#M000334" rel="nofollow noreferrer">Object class</a> so is available on any Ruby object. In the general case it can be used to add a method to a specific instance. e.g. if we have a String <code>str</code> and say:</p> <pre><code>str.instance_eval("def special; size; end") </code></pre> <p>Then this will alias <code>special</code> to <code>size</code> just for <code>str</code> but not for any other String object:</p> <pre><code>irb(main):019:0&gt; "other".special NoMethodError: undefined method `special' for "other":String from (irb):19 </code></pre> <p>To understand what is going on with String.instance_eval remember that the class String is itself an object (an instance of the class <code>Class</code>) and that there is such a singleton instance object of every class defined. When you use <code>String.instance_eval</code> you are evaluating the given code in the context of the <code>String</code> instance object. i.e. it is equivalent to reopening String's metaclass and typing the code passed e.g.</p> <pre><code>class String class &lt;&lt; self def empty '' end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload