Home » Ruby programming

Array.combination() Method with Example in Ruby

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

Ruby Array.combination() Method

In the last article, we have seen how we can make use of the Array.collect method. Array.collect method is quite similar to Array.map methods because both the methods are used to create some modifications in the Array instance. We have also learnt the destructive and non-destructive version of the Array.collect method. In this article, we will see how we can implement Array.combination method? We will understand them with the help of its syntax and program code.

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 will take elements from the Array instance and make 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:

    combination(n) { |c| block }

Argument(s):

This method only requires one argument. This argument decides the number of combinations possible from the elements of Array instance.

Example 1:

=begin
  Ruby program to demonstrate combination method
=end

# array
a = [1, 2, 3, 4]

print a.combination(1).to_a  
puts ""

print a.combination(2).to_a
puts ""  

print a.combination(3).to_a  
puts ""

print a.combination(4).to_a  
puts ""

print a.combination(0).to_a  
puts ""

print a.combination(5).to_a  

Output

[[1], [2], [3], [4]]
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
[[1, 2, 3, 4]]
[[]]
[]

Explanation:

In the above code, you can observe that this method is used to create combinations of the Array elements. Combinations are being created based on the argument passed inside the method. Everything was going fine until the argument 4 passes because there is the presence of 4 elements in the Array. When the number 0 is passed, it created an empty Array instance or you can say that an empty Array has been returned and when the number 5 is passed then an empty Array has been returned because there is not the presence of five elements in the Array instance.

Example 2:

=begin
  Ruby program to demonstrate combination method
=end

# array 
a = ["Sangita", "Babita", "Rashmi"]

print a.combination(1).to_a  
puts ""

print a.combination(2).to_a
puts ""  

print a.combination(3).to_a  
puts ""

print a.combination(4).to_a  
puts ""

print a.combination(0).to_a  
puts ""

print a.combination(5).to_a  

Output

[["Sangita"], ["Babita"], ["Rashmi"]]
[["Sangita", "Babita"], ["Sangita", "Rashmi"], ["Babita", "Rashmi"]]
[["Sangita", "Babita", "Rashmi"]]
[]
[[]]
[]

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 combinations.



Comments and Discussions!

Load comments ↻





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