<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Episode 2 - class &lt;&lt; self' | Pragmatic Forums</title>
    <link>http://forums.pragprog.com/forums/77/topics/657</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Episode 2 - class &amp;lt;&amp;lt; self posted by Bharat Ruparel @ Sat, 12 Jul 2008 20:32:12 -0000</title>
      <description>&lt;p&gt;Thanks for the tip:&lt;/p&gt;


	&lt;p&gt;I tried it, and it prints:&lt;/p&gt;


	&lt;p&gt;Bharat&lt;/p&gt;


	&lt;p&gt;Next, to see how it differs when you defined a method in it and inlcude it in a class, I created the following test scenario:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
module Bharat
    p self
    def method1
      p self
    end
end

class Class1
  include Bharat  
end

m1 = Class1.new.method1

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

	&lt;p&gt;This prints the following:&lt;/p&gt;


	&lt;p&gt;Bharat
#&amp;lt;class1:0x27b0384&gt;&lt;/p&gt;


	&lt;p&gt;Next, I tried the following code:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
module Bharat
    p self
    def method1
      p self
    end
end

class Class1
  extend Bharat  
end

m1 = Class1.method1

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

	&lt;p&gt;This prints the following result:&lt;/p&gt;


	&lt;p&gt;Bharat&lt;br /&gt;Class1&lt;/p&gt;


	&lt;p&gt;When I compare and contrast these results.  I am thinking that a module is nothing but a class that cannot be instantiated. Also, a method in a module can be an instance method or a class method depending on whether the module is included in the class or extended.  Fundamentally, it is just a grouping of similar behavior that can be shared across different classes.  Is that a correct way of approaching it?&lt;/p&gt;


	&lt;p&gt;Bharat&lt;/p&gt;</description>
      <pubDate>Sat, 12 Jul 2008 20:32:12 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:657:3587</guid>
      <author>Bharat Ruparel</author>
      <link>http://forums.pragprog.com/forums/77/topics/657</link>
    </item>
    <item>
      <title>Episode 2 - class &amp;lt;&amp;lt; self posted by Dave Thomas @ Sat, 12 Jul 2008 15:49:52 -0000</title>
      <description>&lt;blockquote&gt;
		&lt;p&gt;I use NetBeans for my Ruby and Ruby on Rails work and its debugger is very helpful in steppig through the program code. I add a &#8220;watch&#8221; on the &#8220;self&#8221; variable and it is fun to see it change as control moves from line to line.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;blockquote&gt;
		&lt;p&gt;One question I have is when would self become a module in these examples? I see it bounce between the Class names and Object (at the top level).&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Try:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
module Bharat
    p self
end
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Sat, 12 Jul 2008 15:49:52 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:657:3584</guid>
      <author>Dave Thomas</author>
      <link>http://forums.pragprog.com/forums/77/topics/657</link>
    </item>
    <item>
      <title>Episode 2 - class &amp;lt;&amp;lt; self posted by Bharat Ruparel @ Sat, 12 Jul 2008 02:12:34 -0000</title>
      <description>&lt;p&gt;Thanks.  I am finding that I have to go back and review the episodes multiple times to get a better understanding of the concepts.  Every time I do that, I take a few steps forward, but sometimes, it is going backwards as well.&lt;/p&gt;


	&lt;p&gt;I see that I have to start using the reference section of your book more often.  The attr_accessor method is clearly documented as a private instance method there.&lt;/p&gt;


	&lt;p&gt;I use NetBeans for my Ruby and Ruby on Rails work and its debugger is very helpful in steppig through the program code.  I add a &amp;#8220;watch&amp;#8221; on the &amp;#8220;self&amp;#8221; variable and it is fun to see it change as control moves from line to line.&lt;/p&gt;


	&lt;p&gt;One question I have is when would self become a module in these examples?  I see it bounce between the Class names and Object (at the top level).&lt;/p&gt;</description>
      <pubDate>Sat, 12 Jul 2008 02:12:34 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:657:3582</guid>
      <author>Bharat Ruparel</author>
      <link>http://forums.pragprog.com/forums/77/topics/657</link>
    </item>
    <item>
      <title>Episode 2 - class &amp;lt;&amp;lt; self posted by Dave Thomas @ Fri, 11 Jul 2008 04:09:33 -0000</title>
      <description>&lt;p&gt;Two problems:&lt;/p&gt;


	&lt;p&gt;First, attr_accessor is a provate method, and so can&amp;#8217;t be called with a receiver.&lt;/p&gt;


	&lt;p&gt;Second, &amp;#8216;self&amp;#8217; is the current class, so self.attr_accessor (if it worked) would be the same as attr_accessopr, which isn&amp;#8217;t what you want. You want attr_accessor to go into the ghost singleton class.&lt;/p&gt;


	&lt;p&gt;Dave&lt;/p&gt;</description>
      <pubDate>Fri, 11 Jul 2008 04:09:33 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:657:3570</guid>
      <author>Dave Thomas</author>
      <link>http://forums.pragprog.com/forums/77/topics/657</link>
    </item>
    <item>
      <title>Episode 2 - class &amp;lt;&amp;lt; self posted by Bharat Ruparel @ Fri, 11 Jul 2008 02:40:28 -0000</title>
      <description>&lt;p&gt;Hello David,&lt;br /&gt;You have this nice example of recommended use of class &amp;lt;&amp;lt; self as follows:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Dave
  @count = 0
  class &amp;lt;&amp;lt; self
    attr_accessor :count
  end
  def initialize()
    Dave.count += 1
  end
end

puts "There are #{Dave.count} instances of Dave." 
d1 = Dave.new
d2 = Dave.new
puts "There are #{Dave.count} instances of Dave." 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now following the logic that you explained before this to turn this into self.method syntax, I did the following:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Dave
  @count = 0
  self.attr_accessor :count
  def initialize()
    Dave.count += 1
  end
end

puts "There are #{Dave.count} instances of Dave." 
d1 = Dave.new
d2 = Dave.new
puts "There are #{Dave.count} instances of Dave." 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But when I run this, I get the following error message:&lt;/p&gt;


	&lt;p&gt;main.rb:3: private method `attr_accessor&amp;#8217; called for Dave:Class (NoMethodError)&lt;/p&gt;


	&lt;p&gt;I am not sure if I understand what is going on here.  Why can&amp;#8217;t I use the parallel but clearer syntax that you explained before this.&lt;/p&gt;


	&lt;p&gt;By the way, brilliant example of include and extend in ActiveRecord setting.  You are a really good teacher.&lt;br /&gt;Bharat&lt;/p&gt;</description>
      <pubDate>Fri, 11 Jul 2008 02:40:28 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:77:657:3566</guid>
      <author>Bharat Ruparel</author>
      <link>http://forums.pragprog.com/forums/77/topics/657</link>
    </item>
  </channel>
</rss>
