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)
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:
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:
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).
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:
Ok, it's just immutable (were news for me). Or maybe not. Who knows for sure? 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:
Bignum is immediate too
Ah, I believe you actually mean numerics are *immutable*.