PHP to Ruby
Convert PHP code into Ruby!-
array_flip
Posted on April 19th, 2010 2 commentsThe array_flip() PHP function changes all the keys in an array into values, and all the values into keys.
PHP
$a = array('apple' => 1, 'ibm' => 2, 'sun' => 3); $flipped = array_flip($a); print_r($flipped); /* Array ( [1] => apple [2] => ibm [3] => sun ) */
To replicate this functionality in Ruby, we will use a Hash object, since arrays in Ruby don’t use associative key / value pairs.
Ruby
hash = { "apple" => 1, "ibm" => 2, "sun" => 3 }; flipped = hash.invert; p flipped; # => {1 => "apple", 2 => "ibm", 3 => "sun"}
In PHP, the array_flip() function will over write any conflicting keys. The Ruby invert() method behaves the same - any keys which are the same as other keys will overwrite the previous one.
-
call_user_func
Posted on March 27th, 2010 No commentsThe call_user_func() function in PHP calls a user defined function as specified by the first parameter. You can also use call_user_func() to call an instance method of an object by using an array(instance, methodName) parameter as follows:
PHP
class User{ private $name = null; public function __construct($name){ $this->name = $name; } public function getName(){ return $this->name; } } $user = new User('Shaymol'); echo call_user_func( array($user, 'getName') ); // => Shaymol
To get same behavior in Ruby we can call the send() method of an object as follows:
Ruby
# define a user class class User attr_accessor :name def initialize(name) @name = name.capitalize end end # create a user object user = User.new('Shaymol') # this is similar to user.name, and in PHP similar to call_user_func($obj, 'methodName'); puts user.send(:name) # => Shaymol
This page was contributed by Shaymol. Thanks!
-
array_keys
Posted on February 24th, 2010 No commentsThe array_keys() function in PHP takes an array as it’s argument and returns all the keys in that array (as a numeric array).
PHP
$array = array('go' => 'green', 'stop' => 'red'); var_dump( array_keys($array) ); /* Array ( [0] => go [1] => stop ) */
To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.
Ruby
array = { :go => 'green', :stop => 'red' }; puts array.keys; # => [:go, :stop]
-
array_product
Posted on February 17th, 2010 No commentsThe array_product() PHP function returns the product of all values in an array. In other words, it multiplies all values in the array together, and returns the result as a number.
PHP
$a = array('1', '2', '3'); $product_total = array_product($a); echo $product_total; // => 6
Ruby
a = [ "1", "2", "3" ]; product_total = a.inject {|product, element| product * element } puts product_total; # => 6
-
array_rand
Posted on February 16th, 2010 No commentsThe array_rand() function in PHP randomly selects one or more elements from an array.
PHP
$users = array('john', 'jane', 'tim', 'alex'); $lucky_winner = array_rand($users); echo $lucky_winner; // alex
Ruby
users = [ 'john', 'jane', 'tim', 'alex' ]; lucky_winner = users[rand(users.length)]; puts lucky_winner; # => alex
-
array_reverse
Posted on October 25th, 2009 No commentsThe array_reverse() function in PHP reverses the order of the elements in an array.
PHP
$a = array('php', 'ruby', 'java'); $results = array_reverse($a); print_r($results); /* Array ( [0] => 'java' [1] => 'ruby' [2] => 'php' ) */
To replicate this functionality in Ruby, we can use the reverse method of the Array object.
Ruby
a = [ "php", "ruby", "java" ]; p a.reverse; # => ["java", "ruby", "php"]
-
array_count_values
Posted on October 21st, 2009 No commentsThe array_count_values() function in PHP counts the number of instances of each value in an array. array_count_values() returns an associative array using the values of the input array as keys and their frequency in the input as values.
PHP
$a = array('a', 'b','a', 'c', 'c'); $results = array_count_values($a); print_r($results); /* Array ( [a] => 2 [b] => 1 [c] => 2 ) */
To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.
Ruby
a = [ "a", "b", "a", "c", "c" ]; result = list.inject({}) do |hash, key| hash.include?(key) ? hash[key] += 1 : hash[key] = 1; hash end p result; # => ["a" => 2, "b" => 1, "c" => 2]
-
array_chunk
Posted on October 17th, 2009 2 commentsThe array_chunk() function in PHP splits an array into multiple smaller arrays (array chunks).
PHP
$langs = array('php', 'ruby', 'java', 'perl', 'csharp'); $my_size = 2; var_dump( array_chunk($langs, $my_size) ); /* Array ( [0] => Array ( [0] => php [1] => ruby ) [1] => Array ( [0] => java [1] => perl ) [2] => Array ( [0] => csharp ) ) ) */
Ruby
def array_chunk(full_array, size) number_of_chunks = (full_array.length/size.to_f).ceil; chunks = (1..number_of_chunks).collect { [] } while full_array.any? chunks.each do |a_chunk| a_chunk << full_array.shift if full_array.any? end end chunks end langs = ["php", "ruby", "java", "perl", "csharp"] my_size = 2 p array_chunk(langs, my_size) # => [["php", "perl"], ["ruby", "csharp"], ["java"]]
-
array_shift
Posted on October 14th, 2009 No commentsThe array_shift() function in PHP removes an element from the beginning of an array and returns it.
PHP
$my_array = array('a', 'b', 'c', 'd'); $my_element = array_shift($my_array); print_r($my_element); /* Array ( [0] => a ) */
Ruby
my_array = [ "a", "b", "c", "d" ]; puts my_array.shift; # => ["a"]
-
array_unique
Posted on October 10th, 2009 No commentsThe array_unique() function in PHP takes an array and filters our any duplicates values, returning the array with only unique values present.
PHP
$elements = array('php', 'ruby', 'perl', 'php'); array_unique($elements); print_r($elements); /* Array ( [0] => php [1] => ruby [2] => perl ) */
Ruby
elements = [ "php", "ruby", "perl", "php" ]; puts elements.uniq; # => ["php", "ruby", "perl"]


