Troubleshooting Ruby String Arrays
I got some unexpected results and I can't figure out what's wrong. The code is in Ruby and it's for an exercise in string rotation. The point of the exercise is to take in a string and output an array of strings with the original rotated: Input: Hello Output: ["elloH", "lloHe", "loHelo", "oHell", "Hello"] Here is my code: https://code.sololearn.com/cOHihE90gh67 When I output the value of 'c' right after it's assigned, it's fine. When I output 'input' after 'insert', it's fine. Yet, it doesn't seem to go into the array properly. When I print out the whole array each iteration of the for loop, it shows the current input value for each value. So, for example, iteration 2 results in this: ["lloHe", "lloHe"] It's as if the items in the array are getting their values tied to the 'input' value, so that every time 'input' changes, so does the value in the array. This doesn't make sense to me, though. Why would that be happening? Thanks for any help you can provide! --------------- UPDATE: I wrote a bunch of code, trying to narrow down the problem. I found out that it has something to do with 'slice.' Here's something I wrote illustrating the issue: https://code.sololearn.com/cc6Np1r924ab The code will output what's being added to the array before it's put into the array.. Then, after all the values are added, it will print out the whole array. But, the values are not as they should be. The previous value in the array seems to get tampered with as a result of using 'slice'. --------------- UPDATE: The issue with 'slice' was overcome by using 'clone' to create an intermediary when working with the input. This doesn't completely explain the issue, but it does seem to fix it. I would still appreciate an explanation for the issue encountered, if anyone has one. For the time being, I'm going to include this working example, illustrating the use of 'clone' to compensate for the issue: https://code.sololearn.com/cRBezCjljqmx