Skip to content

Commit 00bfde0

Browse files
authored
Merge pull request rurema#3312 from Watson1978/enumerator-product-and-producer
Enumerator::Product と Enumerator.product を追加 (Ruby 3.2)
2 parents 5205637 + ca60fe5 commit 00bfde0

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

manual/api/_builtin/Enumerator.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,36 @@ p unknown.size # => nil
159159

160160
#@end
161161

162+
#@since 3.2
163+
### def product(*enums) -> Enumerator::Product
164+
### def product(*enums) { |elts| ... } -> nil
165+
166+
与えた [c:Enumerable] なオブジェクトの直積(デカルト積)を列挙する Enumerator を作って返します。
167+
168+
[m:Enumerator::Product.new] と同じです。
169+
ブロックを与えた場合は、各要素の配列をブロックに渡して繰り返し、nil を返します。
170+
171+
- **param** `enums` -- 直積を取る [c:Enumerable] なオブジェクトを指定します。
172+
173+
```ruby
174+
e = Enumerator.product(1..3, [4, 5])
175+
p e.to_a # => [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
176+
p e.size # => 6
177+
```
178+
179+
```ruby title="例: ブロックを与えた場合"
180+
Enumerator.product(1..2, ["a", "b"]) do |i, s|
181+
p [i, s]
182+
end
183+
# => [1, "a"]
184+
# [1, "b"]
185+
# [2, "a"]
186+
# [2, "b"]
187+
```
188+
189+
- **SEE** [c:Enumerator::Product]
190+
#@end
191+
162192
## Methods
163193

164194
### def +(enum) -> Enumerator::Chain
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
library: _builtin
3+
since: "3.2"
4+
---
5+
# class Enumerator::Product < Enumerator
6+
7+
複数の [c:Enumerable] なオブジェクトの直積(デカルト積)を列挙するためのクラス。
8+
9+
このクラスのオブジェクトは [m:Enumerator.product] から作られます。
10+
11+
各要素は、与えたオブジェクトの数と同じ大きさの配列になります。
12+
右側のオブジェクトほど内側のループになり、最後のオブジェクトが最も速く進みます。
13+
14+
```ruby title="例"
15+
e = Enumerator::Product.new(1..2, ["a", "b"])
16+
e.each do |i, s|
17+
p [i, s]
18+
end
19+
# => [1, "a"]
20+
# [1, "b"]
21+
# [2, "a"]
22+
# [2, "b"]
23+
```
24+
25+
## Class Methods
26+
27+
### def new(*enums) -> Enumerator::Product
28+
29+
与えた [c:Enumerable] なオブジェクトの直積を列挙する Enumerator を作って返します。
30+
31+
- **param** `enums` -- 直積を取る [c:Enumerable] なオブジェクトを指定します。
32+
33+
```ruby title="例"
34+
e = Enumerator::Product.new(1..3, [4, 5])
35+
p e.to_a # => [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
36+
p e.size # => 6
37+
```
38+
39+
- **SEE** [m:Enumerator.product]
40+
41+
## Instance Methods
42+
43+
### def each { |*args| ... } -> self
44+
### def each -> Enumerator
45+
46+
各オブジェクトの直積の要素を、配列としてブロックに渡して繰り返します。
47+
48+
各オブジェクトに対しては each ではなく each_entry を呼び出します。
49+
そのため、N 個のオブジェクトの直積は、各繰り返しでちょうど N 要素の配列になります。
50+
51+
オブジェクトを1つも与えずに作った場合は、空の引数リストでブロックを1回だけ呼びます。
52+
53+
ブロックを渡さない場合は [c:Enumerator] を返します。
54+
55+
```ruby title="例"
56+
e = Enumerator::Product.new(1..2, ["a", "b"])
57+
e.each do |i, s|
58+
p [i, s]
59+
end
60+
# => [1, "a"]
61+
# [1, "b"]
62+
# [2, "a"]
63+
# [2, "b"]
64+
```
65+
66+
### def inspect -> String
67+
68+
self を人間が読みやすい形式の文字列にして返します。
69+
70+
```ruby title="例"
71+
e = Enumerator::Product.new(1..3, [4, 5])
72+
p e.inspect # => "#<Enumerator::Product: [1..3, [4, 5]]>"
73+
```
74+
75+
### def rewind -> self
76+
77+
列挙状態を巻き戻します。
78+
79+
self が持つ各オブジェクトに対して、逆順で rewind メソッドを呼びます。
80+
ただし rewind メソッドを持たないオブジェクトに対しては呼びません。
81+
82+
### def size -> Integer | Float::INFINITY | nil
83+
84+
直積の要素数を返します。
85+
86+
各オブジェクトのサイズの積を返します。
87+
サイズが分からないオブジェクトが含まれる場合は nil を、
88+
無限に続くオブジェクトが含まれる場合は [m:Float::INFINITY] を返します。
89+
90+
```ruby title="例"
91+
p Enumerator::Product.new(1..3, [4, 5]).size # => 6
92+
p Enumerator::Product.new(1.., [4, 5]).size # => Infinity
93+
```

0 commit comments

Comments
 (0)