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

    Posted on October 10th, 2009 RubyLove No comments

    The 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 RubyLove No comments

    The 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 RubyLove No comments

    The 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 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"]