Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • array_combine

    Posted on July 23rd, 2009 RubyLove 2 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"]
  • md5_file

    Posted on May 14th, 2009 RubyLove No comments

    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
  • sha1_file

    Posted on May 11th, 2009 RubyLove No comments

    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
  • sha1

    Posted on May 8th, 2009 RubyLove No comments

    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
  • md5

    Posted on May 5th, 2009 RubyLove No comments

    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
  • array_change_key_case

    Posted on March 28th, 2009 RubyLove No comments

    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 }