difference reverse and reverse! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

difference reverse and reverse!

What is the difference between reverse and reverse! I don't understand it.

5th Jan 2017, 12:00 PM
Andreas Heil
Andreas Heil - avatar
5 Answers
+ 7
for example- arr={1,2,3,4} using arr reverse will have the output- {4,3,2,1} as well as the original array will also be present for future use but in arr reverse! array will become -{4,3,2,1} and the original array i.e. {1,2,3,4} will be destroyed
5th Jan 2017, 4:13 PM
Abhishek Kumar Yadav
Abhishek Kumar Yadav - avatar
+ 4
Thanks Jo go for your super example and the added explanation. Now I know what it means. Thank you!
5th Jan 2017, 1:09 PM
Andreas Heil
Andreas Heil - avatar
+ 3
reverse creates a new array that contains the elements in reverse order as to save the original data adversely reverse! destroys the original and replaces it with the reversed version. usually when you create an array it's to hold an information set, in the given order, by using reverse you can get the data set last to first while leaving the array for other methods to use. but sneaking in a reverse! would change the original set so when you call it, you have to remember that it is backwards. not a problem for an array that isn't being reused, but for database information, and within a team, it's not a good idea since the array is likely to be passed around like the family fruitcake
5th Jan 2017, 12:38 PM
jo go
jo go - avatar
+ 2
arr.[1,2,3,4] puts arr puts arr.reverse puts arr arr.reverse! puts arr
5th Jan 2017, 12:44 PM
jo go
jo go - avatar
+ 1
please provide examples
5th Jan 2017, 12:38 PM
DFX
DFX - avatar