Confusion with Fixnum addition with :inject method

I have confusion with the below code:

## Why does not the output of a is 5 ?

a = 4
p [1].inject(a,:+)
p a # => 4

## The below is expected,why not the above?

h = {:a => 2}
[:a].inject(h,:delete)
p h # => {}

···

--
Posted via http://www.ruby-forum.com/.

I have confusion with the below code:

## Why does not the output of a is 5 ?

a = 4
p [1].inject(a,:+)
p a # => 4

## The below is expected,why not the above?

h = {:a => 2}
[:a].inject(h,:delete)
p h # => {}

Because

    y = x + 1

stores x plus 1 in y, but does not modify the integer stored in the x
variable itself.

On the other hand h.delete modifies the hash stored in the variable h, it
is destructive, the deletion happens in-place.

···

On Mon, Aug 12, 2013 at 12:19 PM, Love U Ruby <lists@ruby-forum.com> wrote:

Fixnum (http://www.ruby-doc.org/core-2.0/Fixnum.html\) objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object. Class: Fixnum (Ruby 2.0.0)

So,
a = b = 0
b += 1
p a
#0

···

On Monday, 12 August 2013 г. at 14:19, Love U Ruby wrote:

I have confusion with the below code:

## Why does not the output of a is 5 ?

a = 4
p [1].inject(a,:+)
p a # => 4

## The below is expected,why not the above?

h = {:a => 2}
[:a].inject(h,:delete)
p h # => {}

--
Posted via http://www.ruby-forum.com/\.

Thanks to all of you for your time to explain me the logic.

···

--
Posted via http://www.ruby-forum.com/.

Fixnum <http://www.ruby-doc.org/core-2.0/Fixnum.html&gt; objects have

immediate value. This means that when they are assigned or passed as
parameters, the actual object is passed, rather than a reference to that
object. Class: Fixnum (Ruby 2.0.0)

But that has no relationship with the question.

    [1].inject(a, :+)

is conceptually short for

    memo = a
    memo = memo + 1 # unrolled loop
    return memo

whereas [:a].inject(h, :delete) unrolls conceptually as

    memo = h
    memo = memo.delete(:a) # unrolled loop
    return memo

The key point is that Hash#delete is destructive so you get h shortened as
a side-effect of the second line. The fact that fixnums are immediate
objects does not really matter, you'd get the same behavior with a bignum
as initial value.

Note that the second inject is an artificial example, since Hash#delete
returns the value associated with the key or nil, and you could not apply
#delete to an integer if the receiver had more items (memo would become an
integer in the second iteration).

···

On Mon, Aug 12, 2013 at 1:10 PM, Alex aka Sinm <sinm.sinm@gmail.com> wrote:

:slight_smile: Bignum is immediate too, well as almost any Number around. I don't even speak about Hash#delete as it works as expected.
I'm speaking about immediates, because you could do the following and get an output of 5, which is now an internal state of non-immediate object:

class A
  def initialize num; @num = num; end
  def + other_num; @num += other_num; self; end
end

a = A.new 4
p [1].inject(a,:+)
p a # => <A:0x007f8efb9f51a0 @num=5>

···

On Monday, 12 August 2013 г. at 16:26, Xavier Noria wrote:

On Mon, Aug 12, 2013 at 1:10 PM, Alex aka Sinm <sinm.sinm@gmail.com (mailto:sinm.sinm@gmail.com)> wrote:

> Fixnum (http://www.ruby-doc.org/core-2.0/Fixnum.html\) objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object. Class: Fixnum (Ruby 2.0.0)

But that has no relationship with the question.

    [1].inject(a, :+)

is conceptually short for

    memo = a
    memo = memo + 1 # unrolled loop
    return memo

whereas [:a].inject(h, :delete) unrolls conceptually as

    memo = h
    memo = memo.delete(:a) # unrolled loop
    return memo

The key point is that Hash#delete is destructive so you get h shortened as a side-effect of the second line. The fact that fixnums are immediate objects does not really matter, you'd get the same behavior with a bignum as initial value.

Note that the second inject is an artificial example, since Hash#delete returns the value associated with the key or nil, and you could not apply #delete to an integer if the receiver had more items (memo would become an integer in the second iteration).

:slight_smile: Bignum is immediate too

It is not.

Anyway, the point is that delete modifies the receiver, and + does not
modify the receiver. The nature of the receiver does not matter for this
question.

···

On Mon, Aug 12, 2013 at 2:45 PM, Alex aka Sinm <sinm.sinm@gmail.com> wrote:

:slight_smile: Bignum is immediate too

Ah, I believe you actually mean numerics are *immutable*.

···

On Mon, Aug 12, 2013 at 2:45 PM, Alex aka Sinm <sinm.sinm@gmail.com> wrote:

Ok, it's just immutable (were news for me). Or maybe not. Who knows for sure? :wink: Anyway of course you're right that its nature of less importance than the nature of concrete operator itself. In my example the same "operator" acts differently and modifies the receiver.

···

On 12.08.2013, at 18:29, Xavier Noria <fxn@hashref.com> wrote:

On Mon, Aug 12, 2013 at 2:45 PM, Alex aka Sinm <sinm.sinm@gmail.com> wrote:

:slight_smile: Bignum is immediate too

Ah, I believe you actually mean numerics are *immutable*.