# Why is it returning false?

**URL:** <https://metaruby.com/t/why-is-it-returning-false/1049>\
**Category:** Ruby Questions\
**Created:** [May 28, 2017, 1:15am UTC](https://metaruby.com/t/why-is-it-returning-false/1049 "2017-05-28T01:15:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marin-cabac](https://metaruby.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@marin-cabac](https://metaruby.com/u/marin-cabac)\
**Post date:** [May 28, 2017, 1:15am UTC](https://metaruby.com/t/why-is-it-returning-false/1049/1 "2017-05-28T01:15:19Z")

</div>

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.

```ruby
def scramble_string(string, positions)   
  i=0 
  while i<string.length 
    string[i]=string[positions[i]]
    i+=1 
  end   
  return string 
end

```

* * *

### Tests for #scramble\_string

```ruby
scramble_string("abcd", [3, 1, 2, 0]) == "dbca"
# => false
scramble_string("markov", [5, 3, 1, 4, 2, 0]) == "vkaorm"
# => false

```

* * *

---

<div class="post-metadata">

**Author:** ![fabiomux](https://metaruby.com/user_avatar/metaruby.com/fabiomux/32/921_2.png) [@fabiomux](https://metaruby.com/u/fabiomux)\
**Post date:** [May 29, 2017, 7:16am UTC](https://metaruby.com/t/why-is-it-returning-false/1049/2 "2017-05-29T07:16:16Z")

</div>

Hi marin, looks like you overwrite the _string_ variable while you assign the chars, i would change the algorithm starting with something like that:

```
def scramble_string(string, positions)
  res = ''
  positions.each do |i|
    res << string[i]
  end
  res
end
```

---

<div class="post-metadata">

**Author:** ![marin-cabac](https://metaruby.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@marin-cabac](https://metaruby.com/u/marin-cabac)\
**Post date:** [May 29, 2017, 3:29pm UTC](https://metaruby.com/t/why-is-it-returning-false/1049/3 "2017-05-29T15:29:28Z")

</div>

Hi fabiomux,

Thanks a lot! I was stuck for 2 days here.  
So that’s the reason for needing an extra variable like ?  
I don’t really understand \<positions.each do [i]\> part, but what I do understand is that I can’t just change the string directly without overwriting it and that’s why I need another variable like that helps me point to the initial string and change it.  
Does that sound right?

Thanks,  
Marin

---

<div class="post-metadata">

**Author:** ![fabiomux](https://metaruby.com/user_avatar/metaruby.com/fabiomux/32/921_2.png) [@fabiomux](https://metaruby.com/u/fabiomux)\
**Post date:** [May 29, 2017, 5:01pm UTC](https://metaruby.com/t/why-is-it-returning-false/1049/4 "2017-05-29T17:01:46Z")

</div>

Yes Marin you need an extra variable (_support variable_ i think is its name in english) where store the scrambled chars keeping the original untouched to don’t lose the original reference to that chars you are moving.

Assuming that ‘Hello’ is the string to scramble and the wanted order is “4,0,2,3,1” we have with your algorithm:

```ruby
# step 1 (i = 0, string = Hello)
string[0] = string[4]
# string = oello
    
# step 2 (i = 1, string = oello)
string[1] = string[0]
# string = oollo

```

… and so on…

Just watch how in the first step you lost the letter “H” which has been replaced with “o”, that means you lost the original string and the next steps will never give you the wanted result.

Using a _support variable_ as i did:

```ruby
# step 1 (i = 0, string = Hello, res = '')
res << string[4]
# res = o, string = Hello

# step 2 (i = 1, string = Hello, res = o)
res << string[0]
# res = oH, string = Hello

```

… and so on…

That way you never lose the original string and finally get the correct result.

If you are looking to a more elegant and Ruby way you can dig among classes and methods and get something like that (just notice how the string variable is still left untouched and a new variable is created by map and given as a result for the latest but not least join method)

```ruby
def scramble_string(string, positions)
  positions.map { |idx| string[idx]}.join
end

```

---

<div class="post-metadata">

**Author:** ![marin-cabac](https://metaruby.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@marin-cabac](https://metaruby.com/u/marin-cabac)\
**Post date:** [May 30, 2017, 12:29am UTC](https://metaruby.com/t/why-is-it-returning-false/1049/5 "2017-05-30T00:29:38Z")

</div>

Thank you so much for the very detailed and helpful explanation!
