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

    Posted on July 29th, 2009 RubyLove No comments

    The is_float() function allows you to check if a particular variable is a float (type).

    PHP

    $number = 6.5;
    var_dump( is_float($number) );
    // => true

    Ruby

    number = 6.5;
    puts number.is_a?(Float);
    # => true
  • 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"]
  • array_pop

    Posted on July 17th, 2009 RubyLove No comments

    The array_pop() function removes the element off the end of an array and returns it’s value.

    PHP

    $my_array = array('java', 'ruby', 'python', 'php');
    echo array_pop($my_array);
    => // php

    Ruby

    my_array = ['java', 'ruby', 'python', 'php'];
    puts my_array.pop
    => # php
  • ucwords

    Posted on July 13th, 2009 RubyLove 1 comment

    The ucwords() function turns the first character of each word in a string to upper-case, if the first character is alphabetic.

    PHP

    echo ucwords("ruby is easy.");
    => // Ruby Is Easy.

    Ruby doesn’t have a function which can capitalize all the words in a string - so to accomplish this it’s a little bit harder. You need to split the string into words, then capitalize the first character of each word, and then finally join all the words back into a string.

    Ruby

    puts "ruby is easy.".split(' ').select {|w| w.capitalize! || w }.join(' ');
    => # Ruby Is Easy.
  • lcfirst

    Posted on July 10th, 2009 RubyLove 2 comments

    The lcfirst() function returns a string, with the first character in lower case - only if the first character is alphabetic.

    PHP

    echo lcfirst("Java is OK.");
    => // java is OK.

    Ruby

    my_string = "Java is OK.";
     
    puts my_string[0,1].downcase + my_string[1..-1];
    => # java is OK.
  • ucfirst

    Posted on July 7th, 2009 RubyLove No comments

    The ucfirst function returns a string, with the first character capitalized - only if the first character is alphabetic.

    PHP

    echo ucfirst("ruby is great!");
    => // Ruby is great!

    Ruby

    puts "ruby is great!".capitalize
    => # Ruby is great!
  • nl2br

    Posted on July 4th, 2009 RubyLove 2 comments

    The nl2br() function inserts HTML <br> tags before any new lines in a string.

    PHP

    echo nl2br("foo has a\n bar");
    /* 
    foo has a<br>
    bar
    */

    Ruby

    p "foo has a\n bar".gsub("\n", "<br>\n")
    # foo has a<br>
    # bar
  • chop

    Posted on July 1st, 2009 RubyLove No comments

    The chop() function is only an alias of rtrim, which strips whitespace (or other characters) from the end of a string.

    For further information about how chop() can be used, see rtrim().