19 10, 2010

array_merge

By | 2010-10-19T11:01:55+00:00 October 19th, 2010|Categories: PHP, Ruby|Tags: , , , , |1 Comment

The 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]
23 07, 2009

array_combine

By | 2009-10-15T22:41:01+00:00 July 23rd, 2009|Categories: PHP, Ruby|Tags: , , , , , |3 Comments

The 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"]
14 05, 2009

md5_file

By | 2009-05-15T23:21:33+00:00 May 14th, 2009|Categories: PHP, Ruby|Tags: , , , , , , |Comments Off on md5_file

The 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
11 05, 2009

sha1_file

By | 2009-05-15T23:12:59+00:00 May 11th, 2009|Categories: PHP, Ruby|Tags: , , , , , , |Comments Off on sha1_file

The 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
8 05, 2009

sha1

By | 2009-05-15T23:22:14+00:00 May 8th, 2009|Categories: PHP, Ruby|Tags: , , , , , |Comments Off on sha1

The 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
5 05, 2009

md5

By | 2009-05-15T23:22:33+00:00 May 5th, 2009|Categories: PHP, Ruby|Tags: , , , , , |Comments Off on md5

The 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
28 03, 2009

array_change_key_case

By | 2009-10-15T22:42:53+00:00 March 28th, 2009|Categories: PHP, Ruby|Tags: , , , , |1 Comment

This 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 }

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.