makersacademy / problem-solving

For problem-solving during the PreCourse
6 stars 2 forks source link

Replacing string characters within an array #175

Closed jamesAforster closed 4 years ago

jamesAforster commented 4 years ago

Hey everyone - I'm having a bit of trouble with chapter 7 question 3. I am trying to figure out how to replace a character within an array, modifying the existing array.

I can change the characters of a string with the index like this:

string = "--C--"
string[1] = "P"
puts string

#==> -PC--

However, I can't figure out how to do the same thing if the string is in an array. I thought it would be something like this:

array =
[["--P--"],
  ["--C--"],
  ["CC-CC"],
  ["CC-CC"]]

array[1][2] = "P"
puts array

#==> 
--P--
--C--
P
CC-CC
CC-CC

So that doesn't do what I want it to. I've tried converting the array element into a string as well but that just gives me a string with the array brackets and the actual string inside that:

puts array[1].to_s
#==> ["--C--"]

Any help much appreciated!