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

    Posted on August 24th, 2009 RubyLove No comments

    The is_long() function in PHP is simply an alias of is_int() - for full details see the is_int() function.

  • is_real

    Posted on August 18th, 2009 RubyLove No comments

    The is_real() function in PHP is simply an alias of is_float() - for full details see the is_float() function.

  • is_numeric

    Posted on August 8th, 2009 RubyLove 3 comments

    The is_numeric() function allows you to check if a particular variable is a numeric string. Numeric strings can consist of a sign, any number of digits, a decimal part and an exponential part.

    PHP

    $my_string = '+1.998';
    var_dump( is_numeric($string1) );
    // => true

    Ruby

    def is_numeric?(num)
        true if Float(num) rescue false;
    end
     
    my_string = '+1.985';
    puts is_numeric?(my_string);
    # => true
  • is_int

    Posted on August 4th, 2009 RubyLove No comments

    The is_int() function allows you to check if a particular variable is an integer (type).

    PHP

    $number = 9.5;
    var_dump( is_int($number) );
    // => false

    Ruby

    number = 9.5;
    puts number.is_a?(Integer);
    # => false
  • is_bool

    Posted on August 1st, 2009 RubyLove No comments

    The is_bool() function allows you to check if a particular variable is a boolean (type).

    PHP

    $my_variable = 1;
    var_dump( is_bool($my_variable) );
    // => false

    Ruby

     my_variable = 1;
    puts my_variable.is_a?(FalseClass) || my_variable.is_a?(TrueClass)
    # => false
  • 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
  • array_sum

    Posted on April 5th, 2009 RubyLove 2 comments

    Returns the sum of values in an array.

    PHP

    $a = array(2, 4, 6, 8);
    echo array_sum($a);
    // => 20

    Ruby

    a_var = [2, 4, 6, 8];
    puts a_var.inject {|sum,x| sum ? sum + x : x };
    # => 20

    In the code above, the return value from the ruby code will be nil if the array used is empty. This is not exactly the same behaviour as PHP, since the array_sum() function will always return a number. We can force ruby to always return a float (or an int) too, even if the array is empty, by adding a bit of type casting to the final result:

    Ruby

    a_var = [];
    puts a_var.inject {|sum,x| sum ? sum + x : x }.to_f;
    # => 0.0
  • pi

    Posted on April 2nd, 2009 RubyLove No comments

    Returns the value of PI to 14 (default) decimal places. This precision can be altered in the php.ini file.

    PHP

    echo pi();
    // => 3.1415926535898

    Ruby

    puts Math::PI;
    # => 3.14159265358979

    In the code above, PI is actually a constant of the Module Math. PHP also has a constant which stores the approximate value of PI - M_PI.

    PHP

    echo M_PI();
    // => 3.1415926535898
  • 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;