Home » Ruby programming

Array.product() Method with Example in Ruby

Ruby Array.product() Method: Here, we are going to learn about the Array.product() method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on January 26, 2020

Array.product() Method

In this article, we will study about Array.product method. You all must be thinking the method must be doing something related to the multiplication of certain elements. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is a public instance method and defined for the Array class in Ruby’s library. This method works in a way that it returns the combinations from all Array instances which are being passed as the arguments or from which the method has been invoked. The length of the returned Array instance is the product of the length of self and the arrays which are being passed in the argument list.

Syntax:

    Array_instance.product(other_ary, ...) -> new_ary

Argument(s) required:

This method requires Array instances as arguments. There is no constraint on the number of arguments passed in the list. You can pass as many arguments as you want to pass.

Example 1:

=begin
  Ruby program to demonstrate product method
=end

# array declaration
Ar = [12,3]
Lang = ["C++","Java","Python"]

puts "Array product implementation."
print Ar.product(Lang)
puts ""

puts "Array elements are:"
print Ar

Output

Array product implementation.
[[12, "C++"], [12, "Java"], [12, "Python"], [3, "C++"], [3, "Java"], [3, "Python"]]
Array elements are:
[12, 3]

Explanation:

In the above code, you can observe that we are creating combinations of two Array instances which are namely Ar and Lang. They both are of different types as one is of String type and another one is of integer type. You can see that the length of the Array instance which is returned is the product of Ar Array instance and Lang Array instance. You can also observe that no impact occurred on the self Array i.e. Ar Array instance.

Example 2:

=begin
  Ruby program to demonstrate product method
=end

# array declaration
Ar = [3]
Lang = ["C++","Java"]

puts "Array product implementation."
print Ar.product(Lang,[12],[true,false])
puts ""

puts "Array elements are:"
print Ar

Output

Array product implementation.
[[3, "C++", 12, true], [3, "C++", 12, false], [3, "Java", 12, true], [3, "Java", 12, false]]
Array elements are:
[3]

Explanation:

In the above example, you can observe that we are creating combinations of Array instances that are associated with different data types. You can also observe that we are passing multiple arguments in the method argument list. You can pass n number of arguments and the length of Array returned is dependent upon the product of the length of Array passes as arguments.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.