+ 1

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

31st Jul 2018, 5:25 AM
sky_blue02
sky_blue02 - avatar
3 Antworten
0
Thank you to Alexander. I just needed more of an explanation of what was going on 'under the hood' so to speak. I actually found a great post that explains what is happening and why. Here is a link to it: http://patshaughnessy.net/2012/1/18/seeing-double-how-ruby-shares-string-values I think that this will be really useful to anyone looking to an in-depth answer to the question that I initially had. Note: For display_string.c (the code of which is at the bottom of the page at the link provided), I had to change %ld to %lx in order to get it to work. It also helped to empty the directory of all other files except for display_string.c and extconf.rb before using the commands to get it up and running. After that, you can create Ruby files in the same directory, include these lines at the top: require_relative 'display_string' debug = Debug.new — then you can use the examples provided on the page without any issue. This provides a hands-on learning experience when approaching the topic of strings in Ruby. Many thanks to the author, Pat Shaughnessy!
1st Aug 2018, 2:10 AM
sky_blue02
sky_blue02 - avatar
+ 1
You use slice! to modify input, and always insert input into the array. So every element in the array points to the same object (they all use the same memory location). To fix this use slice instead of slice!
31st Jul 2018, 3:39 PM
Alexander
Alexander - avatar
+ 1
To illustrate this phenomenon try this code: a = [1]; b = a; a[0] = 2; puts b
31st Jul 2018, 3:41 PM
Alexander
Alexander - avatar