Array#combination():combination()是一种Array类方法,该方法使用一个块进行调用,产生块中所有长度为'n'的组合。
用法: Array.combination() 参数: Arrays in which we want elements to be invoked 返回: all combinations of length 'n' of elements of the array.
代码#1:combination()方法示例
# Ruby code for combination() method 
  
# declaring array 
a = [1, 2, 56, 23] 
  
# combination of length 2 
puts "combination a:#{a.combination(2).to_a}\n\n"
  
# combination of length 3 
puts "combination a:#{a.combination(3).to_a}\n\n"输出:
combination a:[[1, 2], [1, 56], [1, 23], [2, 56], [2, 23], [56, 23]] combination a:[[1, 2, 56], [1, 2, 23], [1, 56, 23], [2, 56, 23]]
代码2:combination()方法示例
# Ruby code for combination() method 
  
# declaring array 
a = [[1, 2, 56, 23], 
    [34, 54, 23, 1]] 
  
# combination of length 2 
puts "collect a:#{a.combination(2).to_a}\n\n"
  
# combination of length 1 
puts "collect a:#{a.combination(1).to_a}\n\n"输出:
collect a:[[[1, 2, 56, 23], [34, 54, 23, 1]]] collect a:[[[1, 2, 56, 23]], [[34, 54, 23, 1]]]
相关用法
- Ruby Array at()用法及代码示例
 - Ruby Array any?()用法及代码示例
 - Ruby Array map!()用法及代码示例
 - Ruby Array dig()用法及代码示例
 - Ruby Array class eql?()用法及代码示例
 - Ruby Array delete_at()用法及代码示例
 - Ruby Array concat()用法及代码示例
 - Ruby Array clear()用法及代码示例
 - Ruby Array compact!()用法及代码示例
 - Ruby Array collect()用法及代码示例
 - Ruby Array rassoc()用法及代码示例
 - Ruby Array count()用法及代码示例
 - Ruby Array intersection用法及代码示例
 - Ruby Array collect!()用法及代码示例
 - Ruby Array cycle()用法及代码示例
 
注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby | Array combination() operation。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
