Module Confusion

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

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

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

Hi,

On Sat, Jul 13, 2013 at 9:55 PM, Michael W. [email protected]
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

On Sat, Jul 13, 2013 at 9:55 AM, Michael W. [email protected]
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

Am 13.07.2013 18:55, schrieb Michael W.:

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

[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