I have confusion with the below code:
Why does not the output of a is 5 ?
a = 4
p [1].inject(a,:+) # => 5
p a # => 4
The below is expected,why not the above?
h = {:a => 2}
[:a].inject(h,:delete)
p h # => {}
I have confusion with the below code:
a = 4
p [1].inject(a,:+) # => 5
p a # => 4
h = {:a => 2}
[:a].inject(h,:delete)
p h # => {}
On Mon, Aug 12, 2013 at 12:19 PM, Love U Ruby [email protected]
wrote:
I have confusion with the below code:
[: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.
Fixnum (Class: Fixnum (Ruby 2.0.0)) 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
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 Mon, Aug 12, 2013 at 1:10 PM, Alex aka Sinm [email protected]
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).
On Mon, Aug 12, 2013 at 2:45 PM, Alex aka Sinm [email protected]
wrote:
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.
Thanks to all of you for your time to explain me the logic.
On Mon, Aug 12, 2013 at 2:45 PM, Alex aka Sinm [email protected]
wrote:
Bignum is immediate too
Ah, I believe you actually mean numerics are immutable.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs