<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in The Ruby Object Model and Metaprogramming | Pragmatic Forums</title>
    <link>http://forums.pragprog.com/forums/77/posts</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <item>
      <title>inherited  - any class posted by Dave Thomas @ Fri, 21 Nov 2008 14:28:20 -0000</title>
      <description>&lt;p&gt;Mike:&lt;/p&gt;


	&lt;p&gt;New classes are subclasses of Object, not Class:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Object
    def self.inherited(by)
        p "inherited by #{by}" 
        super
    end
end

class Dave
end

class Mike
end
 &lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 21 Nov 2008 14:28:20 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:1421:6249</guid>
      <author>Dave Thomas</author>
      <link>http://forums.pragprog.com/forums/77/topics/1421</link>
    </item>
    <item>
      <title>inherited  - any class posted by Michael Stramba @ Fri, 21 Nov 2008 03:14:43 -0000</title>
      <description>&lt;p&gt;Dave,&lt;/p&gt;


	&lt;p&gt;Is it possible to use &amp;#8216;inherited&amp;#8217; to monitor any/every class ?&lt;/p&gt;


	&lt;p&gt;I.e. instead of the hard coded &amp;#8220;Parent&amp;#8221; class in your example, it could monitor any class name, or accept a list of class names to monitor.&lt;/p&gt;


	&lt;p&gt;(Like &amp;#8216;def included&amp;#8217; example using class Module, that monitors any module/class interaction).&lt;/p&gt;


	&lt;p&gt;I tried :&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
class Class
  def self.inherited(by)
    puts "#{self} was inherited by #{by}" 
  end
end

class Child &amp;lt; Class
end

class OtherChild &amp;lt; Class
end
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;But get &amp;#8220;can&amp;#8217;t make subclass of Class&amp;#8221; (TypeError)&lt;/p&gt;


	&lt;p&gt;Mike&lt;/p&gt;</description>
      <pubDate>Fri, 21 Nov 2008 03:14:43 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:1421:6242</guid>
      <author>Michael Stramba</author>
      <link>http://forums.pragprog.com/forums/77/topics/1421</link>
    </item>
    <item>
      <title>My heartfelt thanks to Dave posted by Todd W Crone @ Tue, 28 Oct 2008 14:13:52 -0000</title>
      <description>&lt;p&gt;Add another gold star to Dave&amp;#8217;s screencasts.  I am going through the screencasts a second time now and I am still learning &lt;span class="caps"&gt;SOOOOO&lt;/span&gt; much.  I have been a failed Ruby programmer trying to come over from Java several times.  After learning some Groovy I got going on these screencasts and now I can honestly say I am &lt;span class="caps"&gt;FINALLY&lt;/span&gt; on my way to enlightenment!  God bless you guys at Pragmatic Bookshelf for all your guidance and passion for your craft.  I am sure your efforts are worth far more than $5/episode teaching us novices how to move closer to becoming experts.&lt;/p&gt;</description>
      <pubDate>Tue, 28 Oct 2008 14:13:52 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:529:5682</guid>
      <author>Todd W Crone</author>
      <link>http://forums.pragprog.com/forums/77/topics/529</link>
    </item>
    <item>
      <title>Call old mehod added posted by Jon Stenqvist @ Sun, 19 Oct 2008 09:57:23 -0000</title>
      <description>&lt;p&gt;I want to call the old method added it here is one.&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
  def self.included(klass)
    define_callbacks_for_update_from_xml(klass)
    if klass.instance_methods.include?('update_from_xml')
      wrap_update_from_xml_method(klass)
    else
      (class &amp;lt;&amp;lt; klass; self; end).class_eval do
        old_method_added = instance_method(:method_added)
        define_method(:method_added) do |name|
          old_method_added.bind(self).call(name) if old_method_added
          return unless name == :update_from_xml
          XmlUpdatable.suppress_method_added_hook do
            XmlUpdatable.wrap_update_from_xml_method(self)
          end
        end
      end
    end
  end

But I'm getting this error

/Users/jcodeon/code/equipe/lib/xml_updatable.rb:13:in `bind': singleton method bound for a different object (TypeError) from /Users/jon/code/equipe/lib/xml_updatable.rb:13:in `method_added' from /Users/jon/code/equipe/vendor/rails/activerecord/lib/active_record/associations.rb:1235:in `define_method' from /Users/jon/code/equipe/vendor/rails/activerecord/lib/active_record/associations.rb:1235:in `association_accessor_methods' from /Users/jon/code/equipe/vendor/rails/activerecord/lib/active_record/associations.rb:1005:in `belongs_to' from /Users/jon/code/equipe/app/models/class_section.rb:26 from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `gem_original_require' from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `require' from /Users/jon/code/equipe/vendor/rails/activesupport/lib/active_support/dependencies.rb:148:in `require' ... 53 levels... from /Users/jon/Library/Application Support/TextMate/Bundles/RSpec.tmbundle/Support/lib/spec/../spec/mate/runner.rb:31:in `chdir' from /Users/jon/Library/Application Support/TextMate/Bundles/RSpec.tmbundle/Support/lib/spec/../spec/mate/runner.rb:31:in `run' from /Users/jon/Library/Application Support/TextMate/Bundles/RSpec.tmbundle/Support/lib/spec/../spec/mate/runner.rb:14:in `run_file' from /tmp/textmate-command-8094.rb:3
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 19 Oct 2008 09:57:23 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:1199:5449</guid>
      <author>Jon Stenqvist</author>
      <link>http://forums.pragprog.com/forums/77/topics/1199</link>
    </item>
    <item>
      <title>My heartfelt thanks to Dave posted by Nate Kidwell @ Sun, 21 Sep 2008 16:19:04 -0000</title>
      <description>&lt;p&gt;Want to echo everyone&amp;#8217;s sentiments here.  The only one that I&amp;#8217;ve had any confusion with was #7, and that was due to the deep magic involved rather than Dave Thomas&amp;#8217; presentation itself.&lt;/p&gt;


	&lt;p&gt;A really great series &amp;#8211; he&amp;#8217;s like a civilized version of WhyTheLuckyStiff!&lt;/p&gt;</description>
      <pubDate>Sun, 21 Sep 2008 16:19:04 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:529:4617</guid>
      <author>Nate Kidwell</author>
      <link>http://forums.pragprog.com/forums/77/topics/529</link>
    </item>
    <item>
      <title>Episode 6 posted by Jon Stenqvist @ Tue, 09 Sep 2008 18:23:39 -0000</title>
      <description>&lt;p&gt;Does also const_set ignore instance/class_eval?&lt;/p&gt;


&lt;code&gt;
class Application
end

Application.instance_eval do
  const_set(:TEST_CONST,"JON")
end

Application.class_eval do
  const_set(:TEST_CONST,"JON")
end
&lt;/code&gt;</description>
      <pubDate>Tue, 09 Sep 2008 18:23:39 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:923:4473</guid>
      <author>Jon Stenqvist</author>
      <link>http://forums.pragprog.com/forums/77/topics/923</link>
    </item>
    <item>
      <title>Episode 6 posted by Jon Stenqvist @ Mon, 08 Sep 2008 17:51:17 -0000</title>
      <description>&lt;p&gt;As a novice on this I want rules ;) After getting the metaclass, the preferred would be to use class_eval because I want to add a instance method to the metaclass?&lt;/p&gt;</description>
      <pubDate>Mon, 08 Sep 2008 17:51:17 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:923:4464</guid>
      <author>Jon Stenqvist</author>
      <link>http://forums.pragprog.com/forums/77/topics/923</link>
    </item>
    <item>
      <title>Episode 6 posted by Dave Thomas @ Mon, 08 Sep 2008 03:35:25 -0000</title>
      <description>&lt;p&gt;Jon:&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;define_method&lt;/code&gt; ignores &lt;code&gt;class_&lt;/code&gt; or &lt;code&gt;instance_eval&lt;/code&gt; and always defines an instance method.&lt;/p&gt;


	&lt;p&gt;Dave&lt;/p&gt;</description>
      <pubDate>Mon, 08 Sep 2008 03:35:25 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:923:4459</guid>
      <author>Dave Thomas</author>
      <link>http://forums.pragprog.com/forums/77/topics/923</link>
    </item>
    <item>
      <title>Episode 6 posted by Jon Stenqvist @ Sun, 07 Sep 2008 20:46:03 -0000</title>
      <description>&lt;p&gt;Hi,&lt;/p&gt;


	&lt;p&gt;Thanks for great screencasts.&lt;/p&gt;


	&lt;p&gt;After getting the &amp;#8220;metaclass&amp;#8221; class &amp;lt;&amp;lt; self; self; end. I can&amp;#8217;t find any difference between in using instance_eval and class_eval. I get the feeling that the right method for the job would be instance_eval because it that will create instance methods on the metaclass, and as you told on the screencast there arn&amp;#8217;t really any class_methods.&lt;/p&gt;


	&lt;p&gt;Are there a difference between this two snippets? instance_eval/class_eval.&lt;/p&gt;


&lt;pre&gt;
class Dave
  # def self.const_missing(name)
  #   puts "Missing #{name} in Dave" 
  #   super
  # end
  ghost = class &amp;lt;&amp;lt; self; self; end
  ghost.class_eval do
    original_const_missing = instance_method(:const_missing)
    define_method(:const_missing) do |name|
      puts "Missing #{name} in Dave" 
      original_const_missing.bind(self).call(name)
    end
  end
end
Dave::Fred

class Dave
  ghost = class &amp;lt;&amp;lt; self; self; end
  ghost.*instance_eval* do
    original_const_missing = instance_method(:const_missing)
    define_method(:const_missing) do |name|
      puts "Missing #{name} in Dave" 
      original_const_missing.bind(self).call(name)
    end
  end
end
&lt;/pre&gt;</description>
      <pubDate>Sun, 07 Sep 2008 20:46:03 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:923:4457</guid>
      <author>Jon Stenqvist</author>
      <link>http://forums.pragprog.com/forums/77/topics/923</link>
    </item>
    <item>
      <title>An idea for a future installment posted by Ira Davis @ Wed, 03 Sep 2008 21:20:04 -0000</title>
      <description>&lt;p&gt;Do you suppose you could cover the metadata for an object?  That is if Ruby exposes this.  I&amp;#8217;ve really enjoyed the series and I think it has helped my understanding of object orientation in general and certainly helped my understanding or Ruby.&lt;/p&gt;</description>
      <pubDate>Wed, 03 Sep 2008 21:20:04 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:913:4415</guid>
      <author>Ira Davis</author>
      <link>http://forums.pragprog.com/forums/77/topics/913</link>
    </item>
    <item>
      <title>A bit off-topic posted by Dave Thomas @ Tue, 05 Aug 2008 12:55:56 -0000</title>
      <description>&lt;p&gt;Mathijs:&lt;/p&gt;


	&lt;p&gt;To be fair, I didn&amp;#8217;t say I don&amp;#8217;t do unit testing when metaprogramming. I just said I wasn&amp;#8217;t doing it in the screencast&#8212;after all, it&amp;#8217;s already over 50 minutes, and Mike Clark gave me a 30 minute target&amp;#8230;&lt;/p&gt;


	&lt;p&gt;Remember that metaprogramming is orthogonal to your final code. When you write tests, you write tests for the metaprogramming implementation separately from the tests you write for your use of that metaprogramming. And, because they are unit tests,  it&amp;#8217;s OK to know about the implementation.&lt;/p&gt;


	&lt;p&gt;Dave&lt;/p&gt;</description>
      <pubDate>Tue, 05 Aug 2008 12:55:56 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:768:3934</guid>
      <author>Dave Thomas</author>
      <link>http://forums.pragprog.com/forums/77/topics/768</link>
    </item>
    <item>
      <title>A bit off-topic posted by Mathijs Kwik @ Tue, 05 Aug 2008 07:00:18 -0000</title>
      <description>&lt;p&gt;Dave, in episode 7, you mention you should do unit testing when metaprogramming.&lt;br /&gt;While I try to use unit-testing on most of my normal code, I find it&amp;#8217;s harder to unit-test metaprogramming-code.&lt;br /&gt;I always find myself testing the implementation too much instead of the results.&lt;br /&gt;So my tests are bound too much to the code they test, which is a bad thing.&lt;/p&gt;


	&lt;p&gt;I would be really interested to see you work through episode 7 &lt;em&gt;with&lt;/em&gt; unit-testing. Like you said, this breaks up the flow too much for the original screencast, but maybe you can do a separate one on that?&lt;br /&gt;Or could you make your unit-tests (of the final example at least, maybe of some between-steps) available in the code or somewhere else?&lt;/p&gt;


	&lt;p&gt;This would greatly help me find a nice and safe workflow when metaprogramming.&lt;/p&gt;


	&lt;p&gt;Thanks,&lt;br /&gt;Mathijs&lt;/p&gt;</description>
      <pubDate>Tue, 05 Aug 2008 07:00:18 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:768:3933</guid>
      <author>Mathijs Kwik</author>
      <link>http://forums.pragprog.com/forums/77/topics/768</link>
    </item>
    <item>
      <title>Not sure how to interpret this? posted by Bharat Ruparel @ Fri, 01 Aug 2008 15:52:04 -0000</title>
      <description>&lt;p&gt;Thanks Jim.  I am reading it now.  I found the following post by Ola Bini to be particularly helpful.  Before David&amp;#8217;s screencasts, I could not really understand posts similar to these, I think I do now (at least until the next post asking for help).&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html"&gt;http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Regards,&lt;/p&gt;


	&lt;p&gt;Bharat&lt;/p&gt;</description>
      <pubDate>Fri, 01 Aug 2008 15:52:04 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:714:3885</guid>
      <author>Bharat Ruparel</author>
      <link>http://forums.pragprog.com/forums/77/topics/714</link>
    </item>
    <item>
      <title>Not sure how to interpret this? posted by James Whiteman @ Mon, 28 Jul 2008 16:13:36 -0000</title>
      <description>&lt;p&gt;Bharat,&lt;/p&gt;


	&lt;p&gt;If you haven&amp;#8217;t already checked it out, Why has an excellent article on the above technique:&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html"&gt;http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;In conjunction with Dave&amp;#8217;s screencasts, it&amp;#8217;s a killer tutorial.&lt;/p&gt;</description>
      <pubDate>Mon, 28 Jul 2008 16:13:36 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:714:3820</guid>
      <author>James Whiteman</author>
      <link>http://forums.pragprog.com/forums/77/topics/714</link>
    </item>
    <item>
      <title>Not sure how to interpret this? posted by Bharat Ruparel @ Sat, 26 Jul 2008 02:07:24 -0000</title>
      <description>&lt;p&gt;And this is on Memoization:&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization"&gt;http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Sounds familiar?&lt;/p&gt;</description>
      <pubDate>Sat, 26 Jul 2008 02:07:24 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:714:3795</guid>
      <author>Bharat Ruparel</author>
      <link>http://forums.pragprog.com/forums/77/topics/714</link>
    </item>
    <item>
      <title>Not sure how to interpret this? posted by Bharat Ruparel @ Tue, 22 Jul 2008 19:41:34 -0000</title>
      <description>&lt;p&gt;Thanks.  So essentially it becomes a class method of Object (and inheriting classes) which returns the metaclass &amp;#8216;class&amp;#8217; (or the ghost class) of type class?&lt;/p&gt;


	&lt;p&gt;Check this out!&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://github.com/rails/rails/commit/f4f6e57e8c2a446a4a600576f0caf0fb8921ba13"&gt;http://github.com/rails/rails/commit/f4f6e57e8c2a446a4a600576f0caf0fb8921ba13&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;It seems like Rails 2.2 is embracing this stuff.  I am pretty sure David already knows this, but is not telling us so as not to scare us off!&lt;/p&gt;


	&lt;p&gt;Bharat&lt;/p&gt;</description>
      <pubDate>Tue, 22 Jul 2008 19:41:34 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:714:3737</guid>
      <author>Bharat Ruparel</author>
      <link>http://forums.pragprog.com/forums/77/topics/714</link>
    </item>
    <item>
      <title>Not sure how to interpret this? posted by Dave Thomas @ Tue, 22 Jul 2008 03:36:03 -0000</title>
      <description>&lt;p&gt;We have examples of this in the screencast.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
metaclass = class X
  class &amp;lt;&amp;lt; self
     self
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;simply returns the ghost class (the value of self in the inner class definition). By defining it in object, the method becomes available in all classes.&lt;/p&gt;</description>
      <pubDate>Tue, 22 Jul 2008 03:36:03 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:714:3731</guid>
      <author>Dave Thomas</author>
      <link>http://forums.pragprog.com/forums/77/topics/714</link>
    </item>
    <item>
      <title>Not sure how to interpret this? posted by Bharat Ruparel @ Tue, 22 Jul 2008 01:35:40 -0000</title>
      <description>&lt;p&gt;David,&lt;br /&gt;I am maintaining a Rails App that is using backgroundDrb.  It creates a helper file called dbrb_test_helper.rb in the test directory.  Amongst other things, it has the following piece of code that I cannot understand:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;class Object
 def self.metaclass
  class &amp;lt;&amp;lt; self
   self
  end
 end&lt;br /&gt;...&lt;br /&gt;end&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;So we are adding a &amp;#8220;class&amp;#8221; method to the Object (class? instance?) named &amp;#8216;metaclass&amp;#8217;.  So if I understand your teaching thus far:  There is a ghost class created for the object &amp;#8216;Object&amp;#8217; that has a method metaclass defined in it.  So far so good, but what in the world are the following lines of code are for?&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;class &amp;lt;&amp;lt; self
   self
end&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;I don&amp;#8217;t get it.  Kindly explain.  This is one too many selfs for me.&lt;/p&gt;


	&lt;p&gt;Bharat&lt;/p&gt;</description>
      <pubDate>Tue, 22 Jul 2008 01:35:40 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:714:3729</guid>
      <author>Bharat Ruparel</author>
      <link>http://forums.pragprog.com/forums/77/topics/714</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 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">forums.pragprog.com:77:706:3719</guid>
      <author>Dave Thomas</author>
      <link>http://forums.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">forums.pragprog.com:77:706:3717</guid>
      <author>Victor Moroz</author>
      <link>http://forums.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">forums.pragprog.com:77:706:3716</guid>
      <author>Dave Thomas</author>
      <link>http://forums.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">forums.pragprog.com:77:706:3712</guid>
      <author>Victor Moroz</author>
      <link>http://forums.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">forums.pragprog.com:77:706:3710</guid>
      <author>Victor Moroz</author>
      <link>http://forums.pragprog.com/forums/77/topics/706</link>
    </item>
    <item>
      <title>Using @@var inside class definition posted by Victor Moroz @ Sun, 20 Jul 2008 03:53:11 -0000</title>
      <description>&lt;p&gt;Same example with objects&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Cat
  def initialize
    @@has_tail = true
  end
  def has_tail
    @@has_tail
  end
  def has_tail=(tail)
    @@has_tail = tail
  end
end

felix = Cat.new
tom = Cat.new

puts felix.has_tail # true
puts tom.has_tail   # true

def tom.has_tail=(tail)
  @@has_tail = tail
end

tom.has_tail = false

puts felix.has_tail  # true
puts tom.has_tail    # true !!!

def tom.has_tail
  @@has_tail
end

puts tom.has_tail    #false
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;So it looks like &lt;code&gt;@@var&lt;/code&gt; is relative to the place where method is actually defined. If we have ghost class and method is defined in the ghost class as with def tom.has_tail, then &lt;code&gt;@@has_tail&lt;/code&gt; will be stored in the ghost class, but if methods defined in the class definition itself, then &lt;code&gt;@@has_tail&lt;/code&gt; is stored in the class itself. And Ruby will not check superclass of this ghost class for &lt;code&gt;@@var&lt;/code&gt; defined there, that is why previous example failed with &amp;#8216;uninitialized class variable&amp;#8217; &amp;#8211; inheritance does not work here. But previous example (with Cat.has_tail) is much more tricky &amp;#8211; actually &amp;#8216;class&amp;#8217; of class Cat should point to &amp;#8216;Class&amp;#8217; and &lt;code&gt;@@var&lt;/code&gt; should be stored there, but this is not what happens! In this case we would have &lt;code&gt;@@has_tail&lt;/code&gt; defined in any class and this is not the case. Does it mean we have actually two ghost classes between Cat and Class?&lt;/p&gt;</description>
      <pubDate>Sun, 20 Jul 2008 03:53:11 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:704:3709</guid>
      <author>Victor Moroz</author>
      <link>http://forums.pragprog.com/forums/77/topics/704</link>
    </item>
    <item>
      <title>Using @@var inside class definition posted by Victor Moroz @ Sat, 19 Jul 2008 19:16:02 -0000</title>
      <description>&lt;p&gt;Hello Dave,&lt;/p&gt;


	&lt;p&gt;Thanks for the screencast, it is great. Maybe some day you can dig into using @@ variables, especially on class level. It is used in ActiveRecord and is a bit tricky&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
felix = "felix" 

def felix.has_tail
  @@has_tail
end

def felix.has_tail=(tail)
  @@has_tail = tail
end

felix.has_tail = false
puts felix.has_tail     # false
tom = felix.clone
tom.has_tail = true
puts felix.has_tail     # true

class Cat
  def self.has_tail
    @@has_tail
  end
  def self.has_tail=(tail)
    @@has_tail = tail
  end
end

class Felix &amp;lt; Cat
end

class Tom &amp;lt; Cat
end

Tom.has_tail = true
Felix.has_tail = false
puts Tom.has_tail     # false

&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;But this was the easy part. What I just barely understand&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Cat
  @@has_tail = true
  def self.has_tail
    @@has_tail
  end
end

puts Cat.has_tail  # true

def Cat.has_tail
  @@has_tail
end

puts Cat.has_tail  # uninitialized class variable @@has_tail in Object (NameError)

&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;Guess it has something to do with ghost classes in the second case, so defining singleton within class def as self.method and as Class.method are quite different?&lt;/p&gt;</description>
      <pubDate>Sat, 19 Jul 2008 19:16:02 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:704:3707</guid>
      <author>Victor Moroz</author>
      <link>http://forums.pragprog.com/forums/77/topics/704</link>
    </item>
  </channel>
</rss>
