-
array_fill_keys
Posted on November 11th, 2010 1 commentThe array_fill_keys() function in PHP allows you to populate the values of an array while specifying its keys.
PHP
$keys = array('write', 'debug', 'execute'); $result = array_fill_keys($keys, 'code'); var_export($result); // => array('write' => 'code', 'debug' => 'code', 'execute' => 'code')
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
keys = ['write', 'debug', 'execute'] result = keys.inject({}) do |hash, key| hash[key] = 'code' hash end p result # => {"write"=>"code", "debug"=>"code", "execute"=>"code"}
-
array_fill
Posted on October 13th, 2010 1 commentThe array_fill() function in PHP allows an array to be populated (i.e. filled) with a particular value.
PHP
$a = array_fill(3, 5, 'php'); print_r($a); /* Array ( [3] => php [4] => php [5] => php [6] => php [7] => php ) */
In Ruby this is not really possible because Ruby arrays must have their keys filled in the correct order, i.e. you cant skip assigning values to keys in a Ruby array. As such, the next best thing is to fill those values with nil, or use a hash instead.
Ruby
a = [nil] * 3 + ['php'] * 5; puts a; # => [nil, nil, nil, 'php', 'php', 'php', 'php', 'php']
-
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"]
-
array_values
Posted on October 4th, 2009 No commentsThe array_values() function in PHP takes an array and returns all it’s values as a numeric array.
PHP
$array = array('go' => 'green', 'stop' => 'red'); var_dump( array_values($array) ); /* Array ( [0] => green [1] => red ) */
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.values; # => ["green", "red"]
-
list
Posted on October 1st, 2009 No commentsThe list() function in PHP is used to assign multiple variables as if they were an array. Technically, list() is not a function in PHP, it is a language construct.
Ruby has no real need for a function such as list(), as the same can be achieve using parallel assignment - i.e. assigning comma separated variables to elements of an array using the normal assignment operator.
PHP
$langs = array('php', 'ruby', 'perl'); list($lang1, $lang2, $lang3) = $langs; echo $lang1; // php echo $lang2; // ruby echo $lang3; // perl
Ruby
langs = [ "php", "ruby", "perl" ]; lang1, lang2, lang3 = langs; puts lang1; # php puts lang2; # ruby puts lang3; # perl
-
array_combine
Posted on July 23rd, 2009 3 commentsThe array_combine() function creates an associative array (hash) by using one array for keys and another for values.
PHP
$a = array('python', 'lisp', 'perl'); $b = array('PY', 'LI', 'PE'); $c = array_combine($a, $b); print_r($c); => /* Array ( [python] => PY [lisp] => LI [perl] => PE ) */
To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.
Since there is no exact equivalent of to the array_combine() function in Ruby, we manually create a hash from two different arrays.
Ruby
p1 = ['python', 'lisp', 'perl']; p2 = ['PY', 'LI', 'PE']; # initialize the hash combined_hash = {} # build the hash from 2 different arrays p2.each_with_index do |val, key| combined_hash[p1[key]] = val end # print resulting hash p combined_hash => # ["python"=>"PY", "lisp"=>"LI", "perl"=>"PE"]


