<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.1.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: An introduction to modules : Part 2</title>
	<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/</link>
	<description>Because programming should be fun</description>
	<pubDate>Mon, 22 Mar 2010 00:34:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>

	<item>
		<title>By: Daniel Fischer</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-357</link>
		<author>Daniel Fischer</author>
		<pubDate>Mon, 10 Sep 2007 18:11:18 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-357</guid>
					<description>Hey, that was wonderful! Thank you :)</description>
		<content:encoded><![CDATA[<p>Hey, that was wonderful! Thank you <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
				</item>
	<item>
		<title>By: BK</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-361</link>
		<author>BK</author>
		<pubDate>Tue, 18 Sep 2007 13:24:38 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-361</guid>
					<description>So if I understand this correctly, it's almost like a "preimplemented" interface... Interesting! Finally a plus for Ruby over dotnet! ;)</description>
		<content:encoded><![CDATA[<p>So if I understand this correctly, it&#8217;s almost like a &#8220;preimplemented&#8221; interface&#8230; Interesting! Finally a plus for Ruby over dotnet! <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-419</link>
		<author>Frank</author>
		<pubDate>Mon, 29 Oct 2007 18:54:51 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-419</guid>
					<description>@BK, 

Exactly. I find the term "preimplemented" interface pretty accurate in fact! Thanks</description>
		<content:encoded><![CDATA[<p>@BK, </p>
<p>Exactly. I find the term &#8220;preimplemented&#8221; interface pretty accurate in fact! Thanks</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Vivek Dandage</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-635</link>
		<author>Vivek Dandage</author>
		<pubDate>Wed, 16 Jul 2008 09:14:40 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-635</guid>
					<description>Hey, really cool. Thank you. :)</description>
		<content:encoded><![CDATA[<p>Hey, really cool. Thank you. <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
				</item>
	<item>
		<title>By: jonny_noog</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-638</link>
		<author>jonny_noog</author>
		<pubDate>Thu, 24 Jul 2008 07:00:46 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-638</guid>
					<description>"Finally a plus for Ruby over dotnet!"

...Finally?</description>
		<content:encoded><![CDATA[<p>&#8220;Finally a plus for Ruby over dotnet!&#8221;</p>
<p>&#8230;Finally?</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-807</link>
		<author>How to display a collection grouped by an attribute value in Rails</author>
		<pubDate>Mon, 24 Nov 2008 11:36:43 +0000</pubDate>
		<guid>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>[&#8230;] is a job for Enumerable#group_by (Enumerable is a module that is mixed in the Array [&#8230;]</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: wdj</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-856</link>
		<author>wdj</author>
		<pubDate>Tue, 21 Jul 2009 13:58:24 +0000</pubDate>
		<guid>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: Pancho</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-874</link>
		<author>Pancho</author>
		<pubDate>Tue, 24 Nov 2009 05:42:19 +0000</pubDate>
		<guid>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: freenight</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-875</link>
		<author>freenight</author>
		<pubDate>Tue, 24 Nov 2009 07:31:00 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-875</guid>
					<description>thanks Frank, got it. but what's about the 'instance variables' 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: freenight</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-876</link>
		<author>freenight</author>
		<pubDate>Tue, 24 Nov 2009 07:32:13 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-876</guid>
					<description>wow ... that's not my avatar..</description>
		<content:encoded><![CDATA[<p>wow &#8230; that&#8217;s not my avatar..</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-880</link>
		<author>Frank</author>
		<pubDate>Fri, 27 Nov 2009 15:53:49 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-880</guid>
					<description>Thanks guys, I'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 "module level" 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 "instance" refers to the module itself
    @x = "bla bla bla"
  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 "module level" methods, these variables are only available in the module itself.

Now, if you want to use instance variables where an "instance" 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 'self.' part in the method definition. It means that this 
    #method will reside into the client class instance and NOT in the module itself
    @x = "bla bla bla"
  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 "bla bla bla"</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-881</link>
		<author>freenight</author>
		<pubDate>Sat, 28 Nov 2009 05:16:06 +0000</pubDate>
		<guid>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'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-882</link>
		<author>Frank</author>
		<pubDate>Sat, 28 Nov 2009 13:52:59 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/an-introduction-to-modules-part-2/#comment-882</guid>
					<description>freenight, Exactly. 

I'll try to sum it up in a more concise way :

When you are in a module and define a "module method" (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 "instance method" (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>
</channel>
</rss>
