Skip to content

Instantly share code, notes, and snippets.

@kachick
Created July 19, 2015 16:44
Show Gist options
  • Save kachick/4c53ed4eed9a4b590f4d to your computer and use it in GitHub Desktop.
Save kachick/4c53ed4eed9a4b590f4d to your computer and use it in GitHub Desktop.
Ruby 全ての組み合わせ
# coding: us-ascii
# Copyright (c) 2015 Kenichi Kamiya
class Array
def all_combinations(&block)
return to_enum __callee__ unless block_given?
size.downto(1).map { |n| combination(n, &block) }
end
end
p [1, 2, 3].all_combinations.to_a #=> [[1, 2, 3], [1, 2], [1, 3], [2, 3], [1], [2], [3]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment