From: Harry Ohlsen Date: 2004-05-13T19:12:02+09:00 Subject: Re: How to make combinations of an array to produce all possible expressions? Erik Terpstra wrote: > I have an array 'conds', which contains some sub-expressions for an > xpath query: > > conds = ["@title='Foo'", "@edition='Bar'", "@date='20040513'"] There's no method I know of, but this seems to work (note that I've explicitly avoided generating an empty set, because you didn't have one in your example, but it should probably be included if we wanted to call this method "subsets" as I have) ... module Enumerable def subsets values = [] (2 << length - 1).times do |n| items = [] length.times do |i| if n[i] == 1 items << self[i] end end if items.length > 0 # I'd omit this test for a real "subsets" values << items end end return values end end conds = ["@title='Foo'", "@edition='Bar'", "@date='20040513'"] conds.subsets.collect { |subset| p subset.join(" and ") }