From: Love U Ruby Date: 2013-02-21T19:19:46+09:00 Subject: How does Array#shuffle! works in ruby? Here I was trying to see how Array#shuffle! works with array objects. So I started playing with it in my IRB: [1,2,3] #=> [1, 2, 3] [1,2,3].shuffle! #=> [3, 1, 2] In the above code I understood how it works. And the below I was trying to play with it more hard-way to see it from every corner. [1,2,5] #=> [1, 2, 5] [1,2,5]<<[1,2,5] #=> [1, 2, 5, [1, 2, 5]] Till now I am good. [1, 2, 5, [1, 2, 5]].shuffle! #=> [5, 1, 2, [1, 2, 5]] With above piece of code I have confusions. So below the questions came in my head: (a) Why shuffle! not worked recursively? As I expected that the output of the inner array [1, 2, 5] also will be shuffled. But not happened. (b) Why does shuffle! not shuffle the element array [1, 2, 5] , rather works only with the 1, 2, 5, elements of the array [1, 2, 5, [1, 2, 5]] ? I thought the output would come as [[1, 2, 5],5, 1, 2]. So why the element array didn't change it's position,rather the normal elements did only? Very interesting behavior it is showing: a=[1,2,4] #=> [1, 2, 4] a<<[7,8] #=> [1, 2, 4, [7, 8]] a.shuffle! #=> [[7, 8], 1, 4, 2] a.shuffle! #=> [4, 1, [7, 8], 2] a.shuffle! #=> [[7, 8], 2, 1, 4] irb(main):006:0> Does the shuffling really follow any order or its a random shuffling? -- Posted via http://www.ruby-forum.com/.