D
D
dvarklad2017-01-19 13:57:07
ruby
dvarklad, 2017-01-19 13:57:07

What is the difference between "i = i + 1" and "i += 1"?

Is there a difference, or is it the same thing, only abbreviated?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim Savichev, 2017-01-19
@dvarklad

No difference. Ruby does not have i++, and to shorten 'i = i +1' you can use 'i +=1'
You can see the documentation, for example here: https://www.tutorialspoint.com/ruby/ruby_operators.htm

A
Astrohas, 2017-01-19
@Astrohas

in my opinion, the first creates a new object equal to i + 1 and returns a reference, the second makes an increment for the original object.

M
Maxim, 2017-01-19
@pudovMaxim

no. i++, i=i+1, i+=1

D
Danil Islamov, 2017-05-26
@Osmon

If I'm not mistaken, then i = i + 1 will create 2 objects in memory, and i += 1 will only add one to the variable already in memory

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question