mardi 4 août 2015

Failing to use numeric characters within Ruby array as indices for a string

I am trying to use the numeric charecters from the array held within the positions argument as indices to access the characters of the string inside the string argument to subsequently print a new string. I have an idea of what I need to do to get it to work, but I am hung up.

Total code thus far:

  def scramble_string(string, positions)
    str = string
    pos = positions.join
    newstr = []
    i = 0
    while i < pos.length do
      return newstr.push(str[pos[i]])
      i += 1
    end
  end
  scramble_string("hello", [2, 3, 4, 5])

I suspect my problem lies within this part of the code...

return newstr.push(str[pos[i]])



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire