I’m just starting to learn Ruby (started two days ago), and something
came up in a practice problem that I just can’t figure out (I tried
debugging to pinpoint the error to no avail). I am experiencing
unexpected behavior when defining new_string = string instead of
new_string = “”. The variable “string” changes along with “new_string”
when I do so, even though there is nothing I can see in the code that
should cause this to happen. (When I define new_string = “”, the
original variable, “string”, doesn’t change as the loop proceeds.) Can
anyone explain what’s going on here?
Problem/my program:
Write a method that takes in a string and an array of indices in the
string. Produce a new string, which contains letters from the input
string in the order specified by the indices of the array of indices.
Difficulty: medium.
def scramble_string(string, positions)
new_string = string
#new_string = “” <-- this fixes the problem
i = 0
while i < string.length
x = positions[i]
new_string[i] = string[x]
#I used these lines for attempted debugging
#puts(positions[i])
#puts(string)
#puts(new_string)
i = i + 1
end
return new_string
end
These are tests to check that your code is working. After writing
your solution, they should all print true.
puts(
'scramble_string(“abcd”, [3, 1, 2, 0]) == “dbca”: ’ +
(scramble_string(“abcd”, [3, 1, 2, 0]) == “dbca”).to_s
)
puts(
'scramble_string(“markov”, [5, 3, 1, 4, 2, 0]) == “vkaorm”): ’ +
(scramble_string(“markov”, [5, 3, 1, 4, 2, 0]) == “vkaorm”).to_s
)