Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • shuffle

    Posted on May 26th, 2009 RubyLove No comments

    Randomizes the order of the elements in an array.

    PHP

    $a = array('a', 'b', 'c', 'd');
    shuffle($a);
    print_r($a);
    /*
    Array (
    	[0] => d
    	[1] => b
    	[2] => c
    	[3] => a
    )
    */

    Ruby

    a = [ "a", "b", "c", "d" ];
    puts a.sort_by{ rand };
    # => ["d", "b", "c", "a"]

    Leave a reply