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

    Posted on September 14th, 2009 RubyLove No comments

    The reset() function in PHP sets the internal pointer of an array to its first element. However in Ruby, there is no internal array pointer - as such Ruby doesn’t have an equivalent to PHP’s reset() function.

  • pos

    Posted on September 11th, 2009 RubyLove No comments

    The pos() function is only an alias of current, which returns the key and value of the element the current array pointer is pointing to. However since Ruby doesn’t have an internal array pointer, this function does not have a Ruby equivalent.

    For further information about pos(), see current().

  • current

    Posted on September 8th, 2009 RubyLove No comments

    The current() function in PHP returns the key and value of the element the current array pointer is pointing to. However in Ruby, there is no internal array pointer - as such Ruby doesn’t have an equivalent to PHP’s current() function.

  • key

    Posted on September 5th, 2009 RubyLove No comments

    The key() function in PHP returns the index element of the element the current array pointer is pointing to. However in Ruby, there is no internal array pointer - as such Ruby doesn’t have an equivalent to PHP’s key() function.

  • end

    Posted on September 2nd, 2009 RubyLove No comments

    The end() function in PHP moves the internal array pointer to the last element in the array. However in Ruby, there is no internal array pointer - as such Ruby doesn’t have an equivalent to PHP’s end() function. However Ruby does have the Array::last() method which returns the last element of an array.

    PHP

    $programming = array('java', 'ruby', 'python', 'php');
    $language = end($programming);
    print_r($language);
    // => php

    Ruby

    programming = ['java', 'ruby', 'python', 'php'];
    language = programming.last;
    puts language;
    => # php
  • next

    Posted on August 30th, 2009 RubyLove No comments

    The next() function in PHP moves the internal array pointer one place forward. However in Ruby, there is no internal array pointer - as such Ruby doesn’t have an equivalent to PHP’s next() function.

  • prev

    Posted on August 27th, 2009 RubyLove No comments

    The prev() function in PHP moves the internal array pointer to the previous element (rewinds). However in Ruby, there is no internal array pointer - as such Ruby doesn’t have an equivalent to PHP’s prev() function.

  • range

    Posted on July 26th, 2009 RubyLove No comments

    The range() function creates an array containing a range of elements in PHP, however in Ruby the Range object is used to handle ranges of integers or strings.

    PHP

    $chars = range('a', 'c');
    print_r($chars);
    // => array(0 => 'a', 1 => 'b', 2 => 'c')

    Ruby

    chars = 'a'..'c'
    chars.each {|item| print "#{item}, " } 
    # => a, b, c,
  • 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_push

    Posted on July 20th, 2009 RubyLove No comments

    The array_push() function pushes elements onto the end of an array (acting in the opposite way to array_pop).

    PHP

    $programming = array('java', 'ruby', 'python', 'php');
    array_push($programming, 'asp');
    print_r($programming);
    => /* 
    Array
    (
        [0] => java
        [1] => ruby
        [2] => python
        [3] => php
        [4] => asp
    )
    */

    Ruby

    programming = ['java', 'ruby', 'python', 'php'];
    programming.push('asp');
    => # ["java", "ruby", "python", "php", "asp"]