find the product of all elements in an array in Ruby
Code snippet on how to find the product of all elements in an array in Ruby
array.inject { |product, element| product * element }
This code uses the inject method on the array object. The inject method takes a block as an argument. The block is composed of two parameters, product and element. The inject method iterates through the array, passing each element to the block. The product parameter accumulates the result of the block, which is a product of product and element successively. The initial value of product is the first element in the array. At the end of the iteration, the accumulated result is the product of all elements in the array.