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.

array_combine

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"]
By | 2009-10-15T22:41:01+00:00 July 23rd, 2009|Categories: PHP, Ruby|Tags: , , , , , |3 Comments

About the Author:

3 Comments

  1. Yury October 23, 2009 at 7:48 am

    I think this will look much better:

    p1 = [‘python’, ‘lisp’, ‘perl’];
    p2 = [‘PY’, ‘LI’, ‘PE’];

    [p1,p2].transpose.inject({}) {|res,pair| res.merge({pair[0] => pair[1] }) }

    => # {“python”=>”PY”, “perl”=>”PE”, “lisp”=>”LI”}

  2. Yury October 23, 2009 at 7:56 am

    And even easier!

    p1 = [‘python’, ‘lisp’, ‘perl’];
    p2 = [‘PY’, ‘LI’, ‘PE’];
    Hash[*[p1,p2].transpose.flatten]

    => # {”python”=>”PY”, “perl”=>”PE”, “lisp”=>”LI”}

  3. Boris Barroso August 2, 2011 at 6:50 pm

    Another way
    p1 = [‘python’, ‘lisp’, ‘perl’]
    p2 = [‘PY’, ‘LI’, ‘PE’]
    Hash[p1.zip(p2)]

Comments are closed.