# Module Confusion

**URL:** <https://www.ruby-forum.com/t/module-confusion/235765>\
**Category:** Ruby\
**Created:** [July 13, 2013, 5:48pm UTC](https://www.ruby-forum.com/t/module-confusion/235765 "2013-07-13T17:48:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [July 13, 2013, 5:48pm UTC](https://www.ruby-forum.com/t/module-confusion/235765/1 "2013-07-13T17:48:27Z")

</div>

The below code works. Question though. How does add\_to\_stack acess the  
@stack instance variable going through the method stack? In this case  
is stack variable looked at as literally the array?

Thanks! Michael the Ruby Newby:)

* * *

module Stacklike

def stack  
@stack ||=[]  
end

def add\_to\_stack(obj)  
stack.push(obj)  
end

end

class Stack  
include Stacklike  
end

s = Stack.new

s.add\_to\_stack(“item one”)  
s.add\_to\_stack(“item two”)

puts s.stack

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 13, 2013, 6:40pm UTC](https://www.ruby-forum.com/t/module-confusion/235765/2 "2013-07-13T18:40:11Z")

</div>

Am 13.07.2013 17:48, schrieb Michael W.:

> def stack  
> include Stacklike  
> end
> 
> s = Stack.new
> 
> s.add\_to\_stack(“item one”)  
> s.add\_to\_stack(“item two”)
> 
> puts s.stack

I’m confused about your confusion.

The #stack method simply returns the @stack array (by reference),  
and push modifies it…

Regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 13, 2013, 6:55pm UTC](https://www.ruby-forum.com/t/module-confusion/235765/3 "2013-07-13T18:55:41Z")

</div>

Okay. Let me try and further explain. With stack.push(obj) I would  
understand if it said @stack.push(obj) but that actually breaks the  
code. In the add\_to\_stack method.

unknown wrote in post #1115353:

> Am 13.07.2013 17:48, schrieb Michael W.:
> 
> > def stack  
> > include Stacklike  
> > end
> > 
> > s = Stack.new
> > 
> > s.add\_to\_stack(“item one”)  
> > s.add\_to\_stack(“item two”)
> > 
> > puts s.stack
> 
> I’m confused about your confusion.
> 
> The #stack method simply returns the @stack array (by reference),  
> and push modifies it…
> 
> Regards,  
> Marcus

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 13, 2013, 7:45pm UTC](https://www.ruby-forum.com/t/module-confusion/235765/4 "2013-07-13T19:45:37Z")

</div>

Hi,

On Sat, Jul 13, 2013 at 9:55 PM, Michael W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Okay. Let me try and further explain. With stack.push(obj) I would  
> understand if it said @stack.push(obj) but that actually breaks the  
> code. In the add\_to\_stack method.

@stack is accessed by a call to the stack method.

Meaning stack.push(obj) is calling the stack method to add an item to  
the instance variable with the same name – @stack.

– Arslan

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 14, 2013, 4:41am UTC](https://www.ruby-forum.com/t/module-confusion/235765/5 "2013-07-14T04:41:52Z")

</div>

On Sat, Jul 13, 2013 at 9:55 AM, Michael W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Okay. Let me try and further explain. With stack.push(obj) I would  
> understand if it said @stack.push(obj) but that actually breaks the  
> code. In the add\_to\_stack method.

This is because the stack method will initialize the @stack instance  
variable if it doesn’t already exist:

def stack  
@stack ||=  
end

The ||= operator is common in ruby code. In this case, it will  
initialize  
the @stack instance variable to a new array if it hasn’t yet been  
initialized. Search the forum for the gritty details on how ||= works  
and  
how it can be interpreted. It is pretty complicated under the covers.

If you try to just access @stack in the add\_to\_stack method, your code  
will  
break because the @stack instance variable has not been initialized.  
But  
accessing it through the stack method makes sure it gets initialized  
when  
it is needed.

Hope that helps,  
Doug

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 14, 2013, 1:36pm UTC](https://www.ruby-forum.com/t/module-confusion/235765/6 "2013-07-14T13:36:54Z")

</div>

Am 13.07.2013 18:55, schrieb Michael W.:

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 14, 2013, 1:38pm UTC](https://www.ruby-forum.com/t/module-confusion/235765/7 "2013-07-14T13:38:41Z")

</div>

Yes. Thanks all. I get how it works now.

---

<div class="post-metadata">

**Author:** ![dezrtdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f4b2a3/32.png) [@dezrtdude](https://www.ruby-forum.com/u/dezrtdude)\
**Post date:** [July 14, 2013, 1:40pm UTC](https://www.ruby-forum.com/t/module-confusion/235765/8 "2013-07-14T13:40:25Z")

</div>

[Sorry for the empty email, an accident…]

Am 13.07.2013 18:55, schrieb Michael W.:

> Okay. Let me try and further explain. With stack.push(obj) I would  
> understand if it said @stack.push(obj) but that actually breaks the  
> code. In the add\_to\_stack method.

Douglas already explained why.

To elaborate further: the following _would_ work,  
but I would prefer your original code:

module Stacklike

def stack  
@stack  
end

def add\_to\_stack(obj)  
if @stack  
@stack.push(obj)  
else  
@stack = [obj]  
end  
end

end

class Stack  
include Stacklike  
end

s = Stack.new

s.add\_to\_stack(“item one”)  
s.add\_to\_stack(“item two”)

puts s.stack
