PHP to Ruby
Convert PHP code into Ruby!
-
shuffle
Posted on May 26th, 2009 No commentsRandomizes 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"]
-
sort
Posted on April 14th, 2009 No commentsSorts an array with elements arranged from lowest to highest.
PHP
$a = array('a', 'b', 'e', 'g', 'c', 'd'); sort($a); print_r($a); /* Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => g ) */
Ruby
a = [ "d", "a", "e", "c", "b" ]; puts a.sort; # => ["a", "b", "c", "d", "e"]
-
count
Posted on April 11th, 2009 No commentsReturns the number of elements in an array.
PHP
$a = array('first' => 1, 'second' => 2); echo count($a); // => 2
Ruby
my_array = [1, 2]; puts my_array.length; # => 2
-
sizeof
Posted on March 5th, 2009 No commentsThe sizeof() function is only an alias of count, which returns the number of elements in an array.
For further information about sizeof(), see count().


