# Propogate variable up to included module

**URL:** <https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617>\
**Category:** Ruby\
**Created:** [May 14, 2010, 8:33pm UTC](https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617 "2010-05-14T20:33:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [May 14, 2010, 8:33pm UTC](https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617/1 "2010-05-14T20:33:58Z")

</div>

Hey folks,

I have a module which I’m using to contain a lot of strings that my  
objects print out. I want to use a module because it seems better to me  
to put the data in a separate location than my object implementation, so  
I can more easily see the logic of my object.

My object builds a set of text files. Each of these text files has  
sections within them that are customised based upon a string handed in  
to the object, such that:

bill = textFile.new(“bill”)

bill.header #returns “This is the header for bill”  
bill.comment #returns “Bill is implemented with the following options:”  
followed by the unique options for bill.

ted = textFile.new(“ted”)  
ted.header #returns “This is the header for ted”  
ted.comment #returns “ted is implemented with the following options:”  
followed by the unique options for ted.

and so on.

Now, what I want is to have my Textfile class include a data module. The  
data module contains all of the strings “This is the header for  
#{variable}”, “#{variable} is implemented with the following options:”.

As such, I need to pass the argument to the textfile’s new method back  
up into the module that the textfile includes.

I guess the question is, is there a way to reference a variable from a  
class in a module that the class will include?

Thanks

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [May 14, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617/2 "2010-05-14T23:51:52Z")

</div>

On May 14, 2:34 pm, James H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class in a module that the class will include?  
> At the instance level yes, and the module level no (though you can  
> work around).

Instance level:

module M  
def m  
@x  
end  
end

class X  
include M  
def initialize  
@x = 10  
end  
end

X.new.m #=\> 10

Class level (trick):

module M  
def self.m  
@x ||= 20  
end  
end

class X  
include M  
def self.m  
anc = ancestors.find{ |a| a.respond\_to?(:m) }  
anc.m if anc  
end  
end

trans.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [May 15, 2010, 7:08pm UTC](https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617/3 "2010-05-15T19:08:38Z")

</div>

module M \> def self.m \> @x ||= 20 \> end \> end I do not think this is necessary \> \> class X \> include M \> def self.m \> anc = ancestors.find{ |a| a.respond\_to?(:m) } \> anc.m if anc replace this with instance\_variable\_get (potentially guarded by an instance\_variable\_defined?)

I wonder however, what this is good for, I am not sure I understood  
what OP wanted  
HTH  
R.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [May 15, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617/4 "2010-05-15T19:25:01Z")

</div>

On May 15, 1:05 pm, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def self.m  
> > anc = ancestors.find{ |a| a.respond\_to?(:m) }  
> > anc.m if anc
> 
> replace this with instance\_variable\_get (potentially guarded by an  
> instance\_variable\_defined?)

How would that work?

> I wonder however, what this is good for, I am not sure I understood  
> what OP wanted

Basically class inheritable attributes.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [May 15, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617/5 "2010-05-15T22:42:52Z")

</div>

On Sat, May 15, 2010 at 7:24 PM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> > I wonder however, what this is good for, I am not sure I understood  
> > what OP wanted
> 
> Basically class inheritable attributes.

That’s exactly what I gave you, good then 🙂  
Cheers  
R.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [May 15, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/propogate-variable-up-to-included-module/188617/6 "2010-05-15T22:17:15Z")

</div>

> Basically class inheritable attributes.

I thought modules sit further up the inheritance chain that classes?  
This isn’t a case of inheriting down, this is a case of propagating back  
up the chain.

It’s entirely possible I misunderstand, but Metaprogramming Ruby  
definitely indicates that a module will sit above a class in an  
inheritance chain.
