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

    Posted on February 17th, 2010 RubyLove No comments

    The array_product() PHP function returns the product of all values in an array. In other words, it multiplies all values in the array together, and returns the result as a number.

    PHP

    $a = array('1', '2', '3');
    $product_total = array_product($a);
    echo $product_total;
    // => 6

    Ruby

    a = [ "1", "2", "3" ];
    product_total = a.inject {|product, element| product * element }
    puts product_total;
    # => 6