Home »
Ruby Tutorial
Ruby Array.repeated_combination() Method
By IncludeHelp Last updated : December 01, 2024
In this article, we will study about Array.repeated_combination() method. You all must be thinking the method must be doing something which is related to creating combinations 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.
Description and Usage
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 will take elements from the Array instance and make repeated combinations according to the number passed in the method and then it returns the Array instance itself. This method does not guarantee the order of the elements yielded. This method is invoked with a block or an Array and the result is being converted into the Array instance with the help of .to_a method.
If you do not provide any block then the enumerator is returned itself.
Syntax
array.repeated_combination(n) { |c| block }
Parameters
This method only requires one argument. This argument decides the number of repeated combinations possible from the elements of Array instance.
Example 1
=begin
Ruby program to demonstrate repeated_combination method
=end
# array declaration
a = [1, 2, 3]
print a.repeated_combination(1).to_a
puts ""
print a.repeated_combination(2).to_a
puts ""
print a.repeated_combination(3).to_a
Output
[[1], [2], [3]]
[[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]
[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
Explanation
In the above code, you can observe that this method is used to create repeated combinations of the Array elements. Repeated combinations are being created on the basis of the argument passed inside the method. Unlike combination method it is not necessary that the argument passed inside the method should be less than or equal to the length of Array instance, here is no any compulsion, n number of combinations can be made and that n can be the multiplication of length of Array instance multiplied by the integer passed in the method.
Example 2
=begin
Ruby program to demonstrate repeated_combination method
=end
# array declaration
a = ["Sangita", "Babita"]
print a.repeated_combination(1).to_a
puts ""
print a.repeated_combination(2).to_a
puts ""
print a.repeated_combination(3).to_a
Output
[["Sangita"], ["Babita"]]
[["Sangita", "Sangita"], ["Sangita", "Babita"], ["Babita", "Babita"]]
[["Sangita", "Sangita", "Sangita"], ["Sangita", "Sangita", "Babita"], ["Sangita", "Babita", "Babita"], ["Babita", "Babita", "Babita"]]
Explanation
In the above example, you can observe that this method works upon String Array instances as well. This method is returning elements after making their repeated combinations. The number of combinations can be predicted by multiplying the length of Array instance with the integer passed inside the method at the time of method invocation.