+ 6
difference reverse and reverse!
What is the difference between reverse and reverse! I don't understand it.
5 Respostas
+ 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
+ 4
Thanks Jo go for your super example and the added explanation. Now I know what it means.
Thank you!
+ 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
+ 2
arr.[1,2,3,4]
puts arr
puts arr.reverse
puts arr
arr.reverse!
puts arr
+ 1
please provide examples