From: Douglas Seifert Date: 2013-07-14T11:40:53+09:00 Subject: Re: Module Confusion --047d7b10cb75ecde0104e16fab36 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jul 13, 2013 at 9:55 AM, Michael W. 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 --047d7b10cb75ecde0104e16fab36 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Sat, Jul 13, 2013 at 9:55 AM, Michael W. <lists@rub= y-forum.com> wrote:
Okay. Let me try and further explain. =A0 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:

=A0 =A0def stac= k
=A0 =A0 =A0@stack ||=3D[]
=A0 =A0end

The ||=3D operator is common in ruby code. =A0In = this case, it will initialize the @stack instance variable to a new array i= f it hasn't yet been initialized. =A0Search the forum for the gritty de= tails on how ||=3D works and how it can be interpreted. =A0It is pretty com= plicated under the covers.

If you try to just access @stack in the add_to_stack me= thod, your code will break because the @stack instance variable has not bee= n initialized. =A0But accessing it through the stack method makes sure it g= ets initialized when it is needed.

Hope that helps,
Doug

--047d7b10cb75ecde0104e16fab36--