要約
複数の Enumerable なオブジェクトの直積(デカルト積)を列挙するためのクラス。
このクラスのオブジェクトは Enumerator.product から作られます。
各要素は、与えたオブジェクトの数と同じ大きさの配列になります。右側のオブジェクトほど内側のループになり、最後のオブジェクトが最も速く進みます。
例e = Enumerator::Product.new(1..2, ["a", "b"])
e.each do |i, s|
p [i, s]
end
# => [1, "a"]
# [1, "b"]
# [2, "a"]
# [2, "b"]
目次
継承しているメソッド
- Enumeratorから継承している特異メソッド
- Enumeratorから継承しているメソッド
- Enumerableから継承しているメソッド
-
- all?
- any?
- chain
- chunk
- chunk_while
- collect
- collect_concat
- compact
- count
- cycle
- detect
- drop
- drop_while
- each_cons
- each_entry
- each_slice
- each_with_index
- each_with_object
- entries
- filter
- filter_map
- find
- find_all
- find_index
- first
- flat_map
- grep
- grep_v
- group_by
- include?
- inject
- lazy
- map
- max
- max_by
- member?
- min
- min_by
- minmax
- minmax_by
- none?
- one?
- partition
- reduce
- reject
- reverse_each
- select
- slice_after
- slice_before
- slice_when
- sort
- sort_by
- sum
- take
- take_while
- tally
- to_a
- to_h
- to_set
- uniq
- zip
特異メソッド
new(*enums) -> Enumerator::ProductRuby 3.2 から[permalink][rdoc][edit]-
与えた Enumerable なオブジェクトの直積を列挙する Enumerator を作って返します。
- [PARAM]
enums: - 直積を取る Enumerable なオブジェクトを指定します。
e = Enumerator::Product.new(1..3, [4, 5]) p e.to_a # => [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]] p e.size # => 6[SEE_ALSO] Enumerator.product
- [PARAM]
インスタンスメソッド
each { |*args| ... } -> selfRuby 3.2 から[permalink][rdoc][edit]each -> Enumerator-
各オブジェクトの直積の要素を、配列としてブロックに渡して繰り返します。
各オブジェクトに対しては each ではなく each_entry を呼び出します。そのため、N 個のオブジェクトの直積は、各繰り返しでちょうど N 要素の配列になります。
オブジェクトを1つも与えずに作った場合は、空の引数リストでブロックを1回だけ呼びます。
ブロックを渡さない場合は Enumerator を返します。
例e = Enumerator::Product.new(1..2, ["a", "b"]) e.each do |i, s| p [i, s] end # => [1, "a"] # [1, "b"] # [2, "a"] # [2, "b"] inspect -> StringRuby 3.2 から[permalink][rdoc][edit]-
self を人間が読みやすい形式の文字列にして返します。
例e = Enumerator::Product.new(1..3, [4, 5]) p e.inspect # => "#<Enumerator::Product: [1..3, [4, 5]]>" rewind -> selfRuby 3.2 から[permalink][rdoc][edit]-
列挙状態を巻き戻します。
self が持つ各オブジェクトに対して、逆順で rewind メソッドを呼びます。ただし rewind メソッドを持たないオブジェクトに対しては呼びません。
size -> Integer | Float::INFINITY | nilRuby 3.2 から[permalink][rdoc][edit]-
直積の要素数を返します。
各オブジェクトのサイズの積を返します。サイズが分からないオブジェクトが含まれる場合は nil を、無限に続くオブジェクトが含まれる場合は Float::INFINITY を返します。
例p Enumerator::Product.new(1..3, [4, 5]).size # => 6 p Enumerator::Product.new(1.., [4, 5]).size # => Infinity