maintainable / php-ruby-reference

PHP functions and their equivalents in Ruby (and Rails).

This URL has Read+Write access

php-ruby-reference / array / array_product.markdown
100644 23 lines (17 sloc) 0.411 kb

array_product

We can get the product of an array of integers using a combination of Ruby's Enumerable#inject method with Fixnum#*.

{{code:php

$numbers = array(2, 2, 2, 2);
$result = array_product($numbers);
print $result;
// => 16

}}

{{code:ruby

result = [2, 2, 2, 2].inject {|product, element| product * element }
puts result
# => 16

}}

{{related:

array/array_sum

}}