• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in Ruby code in finding permutations  RSS feed

 
Abhimanyu Aryan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I have marked the code line which creates problem. Look It prints right value of str but its doesn't store the right value of str in global array $arr. Why so?

 
Liutauras Vilda
Marshal
Posts: 4508
306
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please answer to the next questions, so someone could actually help you:

1. What it prints?
1.1 What you expect it to print?

2. What it stores?
2.1 What you expect it to store?

Please also give us example of input and output.
 
Abhimanyu Aryan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch.

Please answer to the next questions, so someone could actually help you:

1. What it prints?
1.1 What you expect it to print?

2. What it stores?
2.1 What you expect it to store?

Please also give us example of input and output.


sorry I missed that part I am adding output

Output:

123
132
213
231
321
312
["123", "123", "123", "123", "123", "123"]

Expected Output:

123
132
213
231
321
312
["123", "132", "213", "231", "321", "312"]
 
Liutauras Vilda
Marshal
Posts: 4508
306
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try it on my machine to see what happens.
 
Liutauras Vilda
Marshal
Posts: 4508
306
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, clear. Strings are mutable in ruby. So, once you add them all in array they all referring to the same string object. After you add last element to it, your line 13 sets string to its initial form.

Add one extra print statement after line 6 to print an array and you'll see yourself ;) How to make it work in your way? We'll let you to think about it first
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!