From: Robert Klemme Date: 2004-05-13T20:18:51+09:00 Subject: Re: How to make combinations of an array to produce all possible expressions? "Harry Ohlsen" schrieb im Newsbeitrag news:40A349EB.8040002@qiqsolutions.com... > 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 Ah, similar idea but nicer coding. I like especially your calculation of the counting range and int[idx] as bit test. I didn't know that one. Btw, you don't need the test for length 0 if you do for n in 1 ... (2<