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

    Posted on September 28th, 2009 RubyLove No comments

    The in_array() function in PHP checks if a value exists in an array - returning true if it does, or false otherwise.

    The last argument in this function forces a strict type check of the value in PHP. Since Ruby does not consider a Fixnum of 1 and a String of ‘1′ to be the same - it always operates as if the last argument were set to true.

    PHP

    $my_array = array('a', 'b', 'c', 'd');
    if('a', in_array($a), true) {
    	echo 'Found it in the array';
    }
    // => Found it in the array

    Ruby

    my_array = [ "a", "b", "c", "d" ];
    if( my_array.include?('a') ) {
    	 puts 'Found it in the array';
    }
    # => Found it in the array
  • 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.

  • implode

    Posted on June 1st, 2009 RubyLove No comments

    The implode function takes and array and forms a string by concatenating the elements in the array.

    PHP

    $langs = array('python', 'java', 'ruby');
    $string = implode(', ', $langs);
    echo $string;
    // => python, java, ruby

    Ruby

    puts ['perl', 'python', 'java'].join(', ');
    # => python, java, ruby
  • explode

    Posted on May 29th, 2009 RubyLove No comments

    The explode function takes 2 parameters - the first is the delimiter, and the second is the string to be exploded. It returns an array of strings, each of which is a substring of the original, formed by splitting the original string on boundaries formed by the string delimiter.

    PHP

    $my_string = 'perl, python, java';
    $array = explode(', ', $my_string);
    var_dump($a);
    /*
    Array (
    	[0] => perl
    	[1] => python
    	[2] => java
    )
    */

    Ruby

    my_string = 'perl, python, java';
    puts my_string.split(', ');
    # => ["perl", "python", "java"]