<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: An introduction to modules : Part 2</title>
	<atom:link href="http://www.rubyfleebie.com/an-introduction-to-modules-part-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/</link>
	<description>Because programming should be fun</description>
	<lastBuildDate>Thu, 15 Jul 2010 19:48:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: rodnic</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-1018</link>
		<dc:creator>rodnic</dc:creator>
		<pubDate>Fri, 02 Jul 2010 22:33:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-1018</guid>
		<description>Congrats! Great article. I really enjoyed this blog. Bookmarked!</description>
		<content:encoded><![CDATA[<p>Congrats! Great article. I really enjoyed this blog. Bookmarked!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: priya</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-996</link>
		<dc:creator>priya</dc:creator>
		<pubDate>Wed, 09 Jun 2010 03:56:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-996</guid>
		<description>wow ,wonderful explanation of module and mixin . i enjoyed reading it. i learnt d concept very easily by your explanation.</description>
		<content:encoded><![CDATA[<p>wow ,wonderful explanation of module and mixin . i enjoyed reading it. i learnt d concept very easily by your explanation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-882</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Sat, 28 Nov 2009 13:52:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-882</guid>
		<description>freenight, Exactly. 

I&#039;ll try to sum it up in a more concise way :

When you are in a module and define a &quot;module method&quot; (e.g. def self.my_module_method), the execution context is the module itself (which is an object as well). When you are in a module and define an &quot;instance method&quot; (e.g. def my_instance_method), the execution context is the class instance that includes the module. This is like you had pasted all the instance methods from the module in your class. This is why we can say that &lt;em&gt;the instance variables are in the class&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>freenight, Exactly. </p>
<p>I&#8217;ll try to sum it up in a more concise way :</p>
<p>When you are in a module and define a &#8220;module method&#8221; (e.g. def self.my_module_method), the execution context is the module itself (which is an object as well). When you are in a module and define an &#8220;instance method&#8221; (e.g. def my_instance_method), the execution context is the class instance that includes the module. This is like you had pasted all the instance methods from the module in your class. This is why we can say that <em>the instance variables are in the class</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: freenight</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-881</link>
		<dc:creator>freenight</dc:creator>
		<pubDate>Sat, 28 Nov 2009 05:16:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-881</guid>
		<description>got it Frank.

the first form:

module SomeModule
def self.talk
@x = ..

since module themselves are object, classes that get this module included have no access to it&#039;s instance method, thus this @x is visible only in the module itself.

on the other hand, instance methods defined in modules becomes instance methods of object with which the class has got the module mixed in, thus this instance variable is.. well instance variable in
per-object basis,right Frank??

sorry for my pool english..</description>
		<content:encoded><![CDATA[<p>got it Frank.</p>
<p>the first form:</p>
<p>module SomeModule<br />
def self.talk<br />
@x = ..</p>
<p>since module themselves are object, classes that get this module included have no access to it&#8217;s instance method, thus this @x is visible only in the module itself.</p>
<p>on the other hand, instance methods defined in modules becomes instance methods of object with which the class has got the module mixed in, thus this instance variable is.. well instance variable in<br />
per-object basis,right Frank??</p>
<p>sorry for my pool english..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-880</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Fri, 27 Nov 2009 15:53:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-880</guid>
		<description>Thanks guys, I&#039;m glad you liked the post!

freenight,this is a very good question.

Instance variables in modules can belong to either :

1: The module
2: The client class

If you need a variable that will exist at &quot;module level&quot; you will do it this way

module SomeModule
  def self.set_instance_variable
    #this method is not accessible for a client class
    #in this case the term &quot;instance&quot; refers to the module itself
    @x = &quot;bla bla bla&quot;
  end

  def self.get_instance_variable
    @x
  end
end

SomeModule.set_instance_variable
SomeModule.get_instance_variable #output : bla bla bla

Conclusion : When you use instance variables inside &quot;module level&quot; methods, these variables are only available in the module itself.

Now, if you want to use instance variables where an &quot;instance&quot; refers to the client class instance and NOT the module itself, you have to proceed this way.

module SomeModule
  def set_instance_variable
    #only difference is that I removed the &#039;self.&#039; part in the method definition. It means that this 
    #method will reside into the client class instance and NOT in the module itself
    @x = &quot;bla bla bla&quot;
  end

  def get_instance_variable
    @x
  end
end

class SomeClass
  include SomeModule
end

x=SomeClass.new

x.set_instance_variable
x.get_instance_variable #output is &quot;bla bla bla&quot;</description>
		<content:encoded><![CDATA[<p>Thanks guys, I&#8217;m glad you liked the post!</p>
<p>freenight,this is a very good question.</p>
<p>Instance variables in modules can belong to either :</p>
<p>1: The module<br />
2: The client class</p>
<p>If you need a variable that will exist at &#8220;module level&#8221; you will do it this way</p>
<p>module SomeModule<br />
  def self.set_instance_variable<br />
    #this method is not accessible for a client class<br />
    #in this case the term &#8220;instance&#8221; refers to the module itself<br />
    @x = &#8220;bla bla bla&#8221;<br />
  end</p>
<p>  def self.get_instance_variable<br />
    @x<br />
  end<br />
end</p>
<p>SomeModule.set_instance_variable<br />
SomeModule.get_instance_variable #output : bla bla bla</p>
<p>Conclusion : When you use instance variables inside &#8220;module level&#8221; methods, these variables are only available in the module itself.</p>
<p>Now, if you want to use instance variables where an &#8220;instance&#8221; refers to the client class instance and NOT the module itself, you have to proceed this way.</p>
<p>module SomeModule<br />
  def set_instance_variable<br />
    #only difference is that I removed the &#8217;self.&#8217; part in the method definition. It means that this<br />
    #method will reside into the client class instance and NOT in the module itself<br />
    @x = &#8220;bla bla bla&#8221;<br />
  end</p>
<p>  def get_instance_variable<br />
    @x<br />
  end<br />
end</p>
<p>class SomeClass<br />
  include SomeModule<br />
end</p>
<p>x=SomeClass.new</p>
<p>x.set_instance_variable<br />
x.get_instance_variable #output is &#8220;bla bla bla&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: freenight</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-876</link>
		<dc:creator>freenight</dc:creator>
		<pubDate>Tue, 24 Nov 2009 07:32:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-876</guid>
		<description>wow ... that&#039;s not my avatar..</description>
		<content:encoded><![CDATA[<p>wow &#8230; that&#8217;s not my avatar..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: freenight</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-875</link>
		<dc:creator>freenight</dc:creator>
		<pubDate>Tue, 24 Nov 2009 07:31:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-875</guid>
		<description>thanks Frank, got it. but what&#039;s about the &#039;instance variables&#039; in module??</description>
		<content:encoded><![CDATA[<p>thanks Frank, got it. but what&#8217;s about the &#8216;instance variables&#8217; in module??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pancho</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-874</link>
		<dc:creator>Pancho</dc:creator>
		<pubDate>Tue, 24 Nov 2009 05:42:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-874</guid>
		<description>Thanks so much for such a clear and understandable explanation!</description>
		<content:encoded><![CDATA[<p>Thanks so much for such a clear and understandable explanation!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wdj</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-856</link>
		<dc:creator>wdj</dc:creator>
		<pubDate>Tue, 21 Jul 2009 13:58:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-856</guid>
		<description>Alas....I understand Ruby Modules ...Thanks</description>
		<content:encoded><![CDATA[<p>Alas&#8230;.I understand Ruby Modules &#8230;Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: How to display a collection grouped by an attribute value in Rails</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/comment-page-1/#comment-807</link>
		<dc:creator>How to display a collection grouped by an attribute value in Rails</dc:creator>
		<pubDate>Mon, 24 Nov 2008 11:36:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-807</guid>
		<description>[...] is a job for Enumerable#group_by (Enumerable is a module that is mixed in the Array [...]</description>
		<content:encoded><![CDATA[<p>[...] is a job for Enumerable#group_by (Enumerable is a module that is mixed in the Array [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
