You're mistaking updating the variable with mutating the object. Fixnums in
ruby are immutable, they have no methods that can modify the object. In
fact, there's no `+=` method anywhere in ruby; the following lines of code
are equivalent:

  x += 1
  x = x + 1

So your `+=` just calls Fixnum#+, which returns a new object, then the
assignment updates the *_variable_* to refer to the _*new object_*.

Conversely, Array#<< is a method that explicitly modifies the object
in-place. For contrast, try the following example:

  def arr_outer2(a,b)
    c = []
    def inner(aa,bb,cc)
      cc += [aa + bb] # note the difference here
    end
    inner a, b, c
    inner b, a, c
    return c
  end




On 12 June 2014 22:05, gvim <gvimrc / gmail.com> wrote:

> If I can increment an integer:
>
> x = 1
> x += 2
> x  #=> 3
>
> ... why doesn't it work the same inside a method?
>
> gvim
>
>
>
> On 12/06/2014 12:45, Juanjo Conti wrote:
>
>> I guess that because in the second one c is a mutable object that is
>> updated while in the first one is an inmutable one ovewriten.
>>
>> El jun 12, 2014 8:34 a.m., "gvim" <gvimrc / gmail.com
>> <mailto:gvimrc / gmail.com>> escribi:
>>
>>
>>     Why do these accumulators behave differently?
>>
>>     def int_outer(a,b)
>>        c = 0
>>        def inner(aa,bb,cc)
>>          cc += (aa + bb)
>>        end
>>        inner a, b, c
>>        inner b, a, c
>>        return c
>>     end
>>
>>     p int_outer 3,4   #=> 0
>>
>>     def arr_outer(a,b)
>>        c = []
>>        def inner(aa,bb,cc)
>>          cc << (aa + bb)
>>        end
>>        inner a, b, c
>>        inner b, a, c
>>        return c
>>     end
>>
>>     p arr_outer 3,4  #=> [7,7]
>>
>>
>>
>>     gvim
>>
>>


-- 
  Matthew Kerwin
  http://matthew.kerwin.net.au/