Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • 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 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"]
  • array_key_exists

    Posted on April 8th, 2009 RubyLove No comments

    Returns TRUE if the given argument is set in the array. The argument passed in can be any value possible for an array index.

    PHP

    $a = array('first' => 1, 'second' => 2);
    var_dump( array_key_exists('first', $a) );
    // => true

    Ruby

    animals = {:sheep => 1, :cow => 2};
    puts animals.include?(:cow);
    # => true
  • array_change_key_case

    Posted on March 28th, 2009 RubyLove 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 }