本文简要介绍ruby语言中  Array.combination  的用法。
用法
combination(n) {|element| ... } → selfcombination(n) → new_enumerator如果给定,则使用 self 的元素组合调用块;返回 self 。组合的顺序是不确定的。
当给定一个块和一个 in-range 正整数参数 n ( 0 < n <= self.size ) 时,调用具有所有 n -tuple 组合的 self 的块。
例子:
a = [0, 1, 2]
a.combination(2) {|combination| p combination }输出:
[0, 1]
[0, 2]
[1, 2]另一个例子:
a = [0, 1, 2]
a.combination(3) {|combination| p combination }输出:
[0, 1, 2]当n 为零时,使用新的空数组调用块一次:
a = [0, 1, 2]
a1 = a.combination(0) {|combination| p combination }输出:
[]当 n 超出范围(负数或大于 self.size )时,不调用该块:
a = [0, 1, 2]
a.combination(-1) {|combination| fail 'Cannot happen' }
a.combination(4) {|combination| fail 'Cannot happen' }如果没有给出块,则返回一个新的枚举器:
a = [0, 1, 2]
a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>相关用法
- Ruby Array.combination()用法及代码示例
- Ruby Array.compact用法及代码示例
- Ruby Array.collect用法及代码示例
- Ruby Array.concat用法及代码示例
- Ruby Array.collect!用法及代码示例
- Ruby Array.count用法及代码示例
- Ruby Array.cycle()用法及代码示例
- Ruby Array.clear用法及代码示例
- Ruby Array.cycle用法及代码示例
- Ruby Array.push用法及代码示例
- Ruby Array.hash用法及代码示例
- Ruby Array.to_a用法及代码示例
- Ruby Array.to_h用法及代码示例
- Ruby Array.to_s用法及代码示例
- Ruby Array.array + other_array用法及代码示例
- Ruby Array.take()用法及代码示例
- Ruby Array.reject!用法及代码示例
- Ruby Array.flatten!用法及代码示例
- Ruby Array.reject用法及代码示例
- Ruby Array.repeated_permutation()用法及代码示例
- Ruby Array.index()用法及代码示例
- Ruby Array.pack()用法及代码示例
- Ruby Array.rassoc(obj)用法及代码示例
- Ruby Array.values_at()用法及代码示例
- Ruby Array.each用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Array.combination。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
