<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Why cool guys use  obj.instance_eval instead of class &lt;&lt; obj to define singletons?' | Pragmatic Forums</title>
    <link>http://fora.pragprog.com/forums/77/topics/706</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Why cool guys use  obj.instance_eval instead of class &amp;lt;&amp;lt; obj to define singletons? posted by Dave Thomas @ Sun, 20 Jul 2008 22:47:06 -0000</title>
      <description>&lt;p&gt;Victor:&lt;/p&gt;


	&lt;p&gt;I know people use it. I just think they shouldn&amp;#8217;t&amp;#8230; :)&lt;/p&gt;</description>
      <pubDate>Sun, 20 Jul 2008 22:47:06 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:77:706:3719</guid>
      <author>Dave Thomas</author>
      <link>http://fora.pragprog.com/forums/77/topics/706</link>
    </item>
    <item>
      <title>Why cool guys use  obj.instance_eval instead of class &amp;lt;&amp;lt; obj to define singletons? posted by Victor Moroz @ Sun, 20 Jul 2008 19:10:44 -0000</title>
      <description>&lt;p&gt;Actually &lt;code&gt;@@var&lt;/code&gt; can be found in many gems, including ActiveSupport, ActiveRecord, haml etc. Of course usage I showed in this post is quite bizzare, but how are you going to share variable between several instances of the same class if you need to access it from instance methods? Well, through class method and class level &lt;code&gt;@var&lt;/code&gt;, but it requires additional lines of code. And even if you do this for objects, you can&amp;#8217;t do it for classes like it is done in ActiveRecord when all subclasses share the same variable. Maybe it is just wrong use of inheritance, don&amp;#8217;t know.&lt;/p&gt;


	&lt;p&gt;Wow, I just realized that top level works as Object.class_eval block!&lt;/p&gt;</description>
      <pubDate>Sun, 20 Jul 2008 19:10:44 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:77:706:3717</guid>
      <author>Victor Moroz</author>
      <link>http://fora.pragprog.com/forums/77/topics/706</link>
    </item>
    <item>
      <title>Why cool guys use  obj.instance_eval instead of class &amp;lt;&amp;lt; obj to define singletons? posted by Dave Thomas @ Sun, 20 Jul 2008 16:06:19 -0000</title>
      <description>&lt;p&gt;As to your first point: that&amp;#8217;s one reason I think &lt;code&gt;@@var&lt;/code&gt; is unusable in real code. I also suspect that example will no longer work in 1.9. In general, &lt;code&gt;@@var&lt;/code&gt; should be avoided.&lt;/p&gt;


	&lt;p&gt;For the second point: the biggest place the two are different is at the top level of your program&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Sun, 20 Jul 2008 16:06:19 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:77:706:3716</guid>
      <author>Dave Thomas</author>
      <link>http://fora.pragprog.com/forums/77/topics/706</link>
    </item>
    <item>
      <title>Why cool guys use  obj.instance_eval instead of class &amp;lt;&amp;lt; obj to define singletons? posted by Victor Moroz @ Sun, 20 Jul 2008 07:45:20 -0000</title>
      <description>&lt;p&gt;And even more confusing here&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
def String.scream
  puts 'wow'
end

String.instance_eval { scream } # wow
String.class_eval { scream } # wow
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;So when we execute instance_eval and class_eval both blocks are executed in the context of String class (self = String), but &amp;#8216;def method&amp;#8217; does different things! Well, concept &amp;#8216;current class&amp;#8217; is reasonable, but how far this concept goes? It means there are two contexts &amp;#8211; &amp;#8216;self&amp;#8217; for calling methods and accessing variables and &amp;#8216;current class&amp;#8217; for defining methods? Where else these contexts are different?&lt;/p&gt;</description>
      <pubDate>Sun, 20 Jul 2008 07:45:20 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:77:706:3712</guid>
      <author>Victor Moroz</author>
      <link>http://fora.pragprog.com/forums/77/topics/706</link>
    </item>
    <item>
      <title>Why cool guys use  obj.instance_eval instead of class &amp;lt;&amp;lt; obj to define singletons? posted by Victor Moroz @ Sun, 20 Jul 2008 07:11:58 -0000</title>
      <description>&lt;p&gt;Or why one should use class &amp;lt;&amp;lt; obj if you have instance_eval? After all instance_eval is more convenient and is executed in the &amp;#8216;obj&amp;#8217; scope (e.g. you can use instance methods), while class &amp;lt;&amp;lt; obj is something odd as it is executed in the scope of the ghost class. What can be done in this scope except of defining singletons? Well, just about my favorite &lt;code&gt;@@var&lt;/code&gt; &amp;#8211; &lt;code&gt;@@var&lt;/code&gt; defined inside class &amp;lt;&amp;lt; obj becomes common for all classes!&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class &amp;lt;&amp;lt; "dog" 
  @@var = 1
end

class &amp;lt;&amp;lt; Fixnum
  puts @@var     # 1
end

class String
  puts @@var     # 1
end
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;This means there is not ghost class between singleton ghost class and Class while there is between String and Class for example. Confusing&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Sun, 20 Jul 2008 07:11:58 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:77:706:3710</guid>
      <author>Victor Moroz</author>
      <link>http://fora.pragprog.com/forums/77/topics/706</link>
    </item>
  </channel>
</rss>
