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

    Posted on March 31st, 2009 RubyLove No comments

    Returns the argument provided with all alphabetic characters converted to uppercase.

    PHP

    echo strtoupper('Ruby is pure OO');
    // => RUBY IS PURE OO

    Ruby

    puts 'Ruby is pure OO'.upcase;
    # => RUBY IS PURE OO
  • 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 }
  • addslashes

    Posted on March 25th, 2009 RubyLove No comments

    Addslashes takes a string as an argument, and returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote [ ' ], double quote [ " ], backslash [ \ ] and NULL (the NULL byte).

    PHP

    echo addslashes("Here's some money for your lunch");
    // => Here\'s some money for your lunch

    Ruby

    puts "Here's some money for your lunch".gsub(/['"\\\x0]/,'\\\\\0');
    # => Here\'s some money for your lunch

    As you can see above, the easiest way to emulate the PHP function addslashes() in Ruby is to use regular expressions.

  • abs

    Posted on March 20th, 2009 RubyLove No comments

    Returns the absolute value for a number.

    PHP

    echo abs(-4.2);
    // => 4.2;

    Ruby

    puts -4.2.abs;
    # => 4.2;
  • acos

    Posted on March 17th, 2009 RubyLove No comments

    Returns the arc cosine of the argument provided in radians.

    PHP

    echo acos(0.7);
    // => 0.79539883018414;

    Ruby

    puts Math.acos(0.7);
    # => 0.79539883018414;
  • cos

    Posted on March 14th, 2009 RubyLove No comments

    Returns the cosine of the argument provided in radians.

    PHP

    echo cos(0.5);
    // => 0.87758256189037;

    Ruby

    puts Math.cos(0.5);
    # => 0.877582561890373;
  • ceil

    Posted on March 11th, 2009 RubyLove No comments

    Returns the next highest integer value by rounding up the argument provided.

    PHP

    echo ceil(4.1);
    // => 5

    Ruby

    puts 4.1.ceil;
    # => 5
  • strtolower

    Posted on March 8th, 2009 RubyLove No comments

    Returns the argument provided with all alphabetic characters converted to lowercase.

    PHP

    echo strtolower('Ruby is pure OO');
    // => ruby is pure oo

    Ruby

    puts 'Ruby is pure OO'.downcase;
    # => ruby is pure oo
  • sizeof

    Posted on March 5th, 2009 RubyLove No comments

    The sizeof() function is only an alias of count, which returns the number of elements in an array.

    For further information about sizeof(), see count().