-
array_merge
Posted on October 19th, 2010 1 commentThe array_merge() function in PHP merges 2 arrays by appending the second array onto the first array, and returning the resulting array.
The way to do this in Ruby depends on the type of data - if dealing with associative arrays (known as a hash in Ruby), we can use the merge() method of the Ruby Hash class.
PHP
$user_details = array('name' => 'John', 'age' => 20); $account_details = array('credits' => '50', 'id' => 4); $user = array_merge($user_details, $account_details); print_r($user); /* Array ( [name] => John [age] => 20 [credits] => 50 [id] => 4 ) */
Ruby
user_details = { :name => 'John', :age => 20 } account_details = { :credits => 50, :id => 4 } p user_details.merge(account_details); # => {:credits=>50, :name=>"John", :id=>4, :age=>20}
Merging numeric arrays in Ruby is much easier as shown below (PHP example given first).
PHP
$start = array(1, 2, 3); $finish = array(4, 5, 6); $nums = array_merge($start, $finish); print_r($nums); /* Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) */
Ruby
start = [1, 2, 3]; finish = [4, 5, 6]; nums = start + finish; p nums; # => [1, 2, 3, 4, 5, 6]
-
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"]
-
md5_file
Posted on May 14th, 2009 No commentsThe md5_file() function calculates the md5 hash of a file.
PHP
$my_file = '/home/ali/article.txt'; echo md5_file($my_file); // => 745d3dfb68af1b1384aea0125aae5c3f
MD5 encryption can be done using the Digest::MD5 class in Ruby, but to generate a hash for a file, we need to read it’s contents into a variable (string) first.
Ruby
require 'digest/md5'; my_string = File.read('/home/ali/article.txt'); print Digest::MD5.hexdigest(my_string); # => 745d3dfb68af1b1384aea0125aae5c3f
-
sha1_file
Posted on May 11th, 2009 No commentsThe sha1_file() function calculates the sha1 hash of a file.
PHP
$my_file = '/home/ali/article.txt'; echo sha1_file($my_file); // => bef6b2b56f70eb6f2c250e00c855f4c0120832aa
Sha1 encryption can be done using the Digest::SHA1 class in Ruby, but to generate a hash for a file, we need to read it’s contents into a variable (string) first.
Ruby
require 'digest/sha1'; my_string = File.read('/home/ali/article.txt'); print Digest::SHA1.hexdigest(my_string); # => bef6b2b56f70eb6f2c250e00c855f4c0120832aa
-
sha1
Posted on May 8th, 2009 No commentsThe sha1() function calculates the sha1 hash of a string.
PHP
echo sha1('apple'); // => d0be2dc421be4fcd0172e5afceea3970e2f3d940
Sha1 encryption can be done using the Digest::SHA1 class in Ruby. To use this class, we need to require the Digest library first…
Ruby
require 'digest/sha1'; print Digest::SHA1.hexdigest('apple'); # => d0be2dc421be4fcd0172e5afceea3970e2f3d940
-
md5
Posted on May 5th, 2009 No commentsThe md5() function calculates the md5 hash of a string.
PHP
echo md5('apple'); // => 1f3870be274f6c49b3e31a0c6728957f
MD5 encryption can be done using the Digest library in Ruby. To use the Digest library, just require it…
Ruby
require 'digest/md5'; print Digest::MD5.hexdigest('apple'); # => 1f3870be274f6c49b3e31a0c6728957f
-
array_change_key_case
Posted on March 28th, 2009 1 commentThis function changes all keys in an array by returning an array with all keys from argument lowercased or uppercased. Numbered indices are left as is.
PHP
$input_array = array('FirSt' => 1, 'SecOnd' => 4); print_r( array_change_key_case($input_array, CASE_UPPER) ); // => array('FIRST' => 1, 'SECOND' => 4);
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
hash = {'FirSt' => 1, 'SecOnd' => 4} result = hash.inject({}) do |hash, keys| hash[keys[0].upcase] = keys[1] hash end p result # => { 'FIRST' => 1, 'SECOND' => 4 }
PHP, Ruby arrays, hash, keys, lower case, upper case


