Robert Klemme wrote:
> Funny to see how this evolves.  My current vesion looks like this:
> 
> module Enumerable
>   def each_subset(skip_empty = false)
>     for n in (skip_empty ? 1 : 0) ... (1 << size) do
>       subset = []
> 
>       each_with_index do |elem, i|
>         subset << elem if n[i]
>       end
> 
>       yield subset
>     end
> 
>     self
>   end
> 
>   def powerset(skip_empty = false)
>     subsets = []
> 
>     each_subset(skip_empty) { |s| subsets << s }
> 
>     return subsets
>   end
> end

The first version in your latest e-mail didn't work for me.  It just gave me the complete array every time.

I think it's because when n[i] is zero, that's not the same as false, hence the "if" always succeeds.

You just need to change that line to

        subset << elem if (n[i] == 1)

The second version worked fine.

I like all of your clean-ups.  I wonder whether re-coding in C would increase performance?

Anyway, it might be worth creating an RCR to have this added to Enumerable.

Cheers,

Harry O.