ひわだです。

以下の script で collect がちょっと予想外の動作をします。
1.1c0, 1.1c6 を使っています。

module Enumerable
  def mycollect ; a=[]; self.each{|i| a<<i}; a end
end
class CombinationIterator
  include Enumerable
  def initialize(v, c=2); @v, @c = v, c end
  def each(&block); @v.each_combi(@c, [], &block) end
end
class Array
  def each_combi(n=2, st=[], &block)
    if n == 1
      each{|i| block.call(st+[i])}
    else
      for i in 0..length-n
	self[i+1..-1].each_combi(n-1, st+[self[i]], &block)
      end
    end
  end
end

rbc0> c = CombinationIterator.new([1,2,3],2)                
#<CombinationIterator: @v=[1, 2, 3], @c=2>
rbc0> c.each{|i|p i}
[1, 2]
[1, 3]
[2, 3]
nil #予想通り
rbc0> c.collect{|i|i}
[[1, 2], nil, nil, nil, [1, 3], nil, nil, nil, [2, 3], nil, nil, nil] #あれ?
rbc0> c.mycollect{|i|i}
[[1, 2], [1, 3], [2, 3]] #これは予想通り

collect ってこういうもの?…じゃないですよね。

#仕様? ^^;。とりあえず再定義しとくかな…
--
檜田 和浩 ( hiwada / kuee.kyoto-u.ac.jp )