-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnumerable.html
743 lines (421 loc) Β· 20.9 KB
/
Enumerable.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
---
title: Enumerable
layout: default
---
<div class="main">
<div class="banner">
<span>Ruby on Rails 8.0.0</span><br />
<div class="type">Module</div>
<h1>
Enumerable
</h1>
<ul class="files">
<li><a href="../files/activesupport/lib/active_support/core_ext/enumerable_rb.html">activesupport/lib/active_support/core_ext/enumerable.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/object/json_rb.html">activesupport/lib/active_support/core_ext/object/json.rb</a></li>
</ul>
</div>
<div id="bodyContent">
<div id="content">
<h2 id="namespace">Namespace</h2>
<h3 id="class">Class</h3>
<ul>
<li><a href="Enumerable/SoleItemExpectedError.html">Enumerable::SoleItemExpectedError</a></li>
</ul>
<h2 id="methods">Methods</h2>
<ul>
<li>
<a href="#method-i-compact_blank">compact_blank</a>
</li>
<li>
<a href="#method-i-exclude-3F">exclude?</a>
</li>
<li>
<a href="#method-i-excluding">excluding</a>
</li>
<li>
<a href="#method-i-in_order_of">in_order_of</a>
</li>
<li>
<a href="#method-i-including">including</a>
</li>
<li>
<a href="#method-i-index_by">index_by</a>
</li>
<li>
<a href="#method-i-index_with">index_with</a>
</li>
<li>
<a href="#method-i-many-3F">many?</a>
</li>
<li>
<a href="#method-i-maximum">maximum</a>
</li>
<li>
<a href="#method-i-minimum">minimum</a>
</li>
<li>
<a href="#method-i-pick">pick</a>
</li>
<li>
<a href="#method-i-pluck">pluck</a>
</li>
<li>
<a href="#method-i-sole">sole</a>
</li>
<li>
<a href="#method-i-without">without</a>
</li>
</ul>
<!-- Methods -->
<h2 id="instance-public-methods">Instance Public methods</h2>
<div class="method">
<h3 id="method-i-compact_blank">
compact_blank()
</h3>
<div class="description">
<p>Returns a new <code>Array</code> without the blank items. Uses <a href="Object.html#method-i-blank-3F"><code>Object#blank?</code></a> for determining if an item is blank.</p>
<pre><code>[1, "", nil, 2, " ", [], {}, false, true].compact_blank
# => [1, 2, true]
Set.new([nil, "", 1, false]).compact_blank
# => [1]
</code></pre>
<p>When called on a <code>Hash</code>, returns a new <code>Hash</code> without the blank values.</p>
<pre><code>{ a: "", b: 1, c: nil, d: [], e: false, f: true }.compact_blank
# => { b: 1, f: true }
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 184
def compact_blank
reject(&:blank?)
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L184" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-exclude-3F">
exclude?(object)
</h3>
<div class="description">
<p>The negative of the <code>Enumerable#include?</code>. Returns <code>true</code> if the collection does not include the object.</p>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 118
def exclude?(object)
!include?(object)
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L118" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-excluding">
excluding(*elements)
</h3>
<div class="description">
<p>Returns a copy of the enumerable excluding the specified elements.</p>
<pre><code>["David", "Rafael", "Aaron", "Todd"].excluding "Aaron", "Todd"
# => ["David", "Rafael"]
["David", "Rafael", "Aaron", "Todd"].excluding %w[ Aaron Todd ]
# => ["David", "Rafael"]
{foo: 1, bar: 2, baz: 3}.excluding :bar
# => {foo: 1, baz: 3}
</code></pre>
</div>
<div class="aka">
Also aliased as: <a href="Enumerable.html#method-i-without">without</a>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 132
def excluding(*elements)
elements.flatten!(1)
reject { |element| elements.include?(element) }
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L132" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-in_order_of">
in_order_of(key, series, filter: true)
</h3>
<div class="description">
<p>Returns a new <code>Array</code> where the order has been set to that provided in the <code>series</code>, based on the <code>key</code> of the objects in the original enumerable.</p>
<pre><code>[ Person.find(5), Person.find(3), Person.find(1) ].in_order_of(:id, [ 1, 5, 3 ])
# => [ Person.find(1), Person.find(5), Person.find(3) ]
</code></pre>
<p>If the <code>series</code> include keys that have no corresponding element in the <a href="Enumerable.html"><code>Enumerable</code></a>, these are ignored. If the <a href="Enumerable.html"><code>Enumerable</code></a> has additional elements that arenβt named in the <code>series</code>, these are not included in the result, unless the <code>filter</code> option is set to <code>false</code>.</p>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 197
def in_order_of(key, series, filter: true)
if filter
group_by(&key).values_at(*series).flatten(1).compact
else
sort_by { |v| series.index(v.public_send(key)) || series.size }.compact
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L197" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-including">
including(*elements)
</h3>
<div class="description">
<p>Returns a new array that includes the passed elements.</p>
<pre><code>[ 1, 2, 3 ].including(4, 5)
# => [ 1, 2, 3, 4, 5 ]
["David", "Rafael"].including %w[ Aaron Todd ]
# => ["David", "Rafael", "Aaron", "Todd"]
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 112
def including(*elements)
to_a.including(*elements)
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L112" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-index_by">
index_by()
</h3>
<div class="description">
<p>Convert an enumerable to a hash, using the block result as the key and the element as the value.</p>
<pre><code>people.index_by(&:login)
# => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
people.index_by { |person| "#{person.first_name} #{person.last_name}" }
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 52
def index_by
if block_given?
result = {}
each { |elem| result[yield(elem)] = elem }
result
else
to_enum(:index_by) { size if respond_to?(:size) }
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L52" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-index_with">
index_with(default = (no_default = true))
</h3>
<div class="description">
<p>Convert an enumerable to a hash, using the element as the key and the block result as the value.</p>
<pre><code>post = Post.new(title: "hey there", body: "what's up?")
%i( title body ).index_with { |attr_name| post.public_send(attr_name) }
# => { title: "hey there", body: "what's up?" }
</code></pre>
<p>If an argument is passed instead of a block, it will be used as the value for all elements:</p>
<pre><code>%i( created_at updated_at ).index_with(Time.now)
# => { created_at: 2020-03-09 22:31:47, updated_at: 2020-03-09 22:31:47 }
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 75
def index_with(default = (no_default = true))
if block_given?
result = {}
each { |elem| result[elem] = yield(elem) }
result
elsif no_default
to_enum(:index_with) { size if respond_to?(:size) }
else
result = {}
each { |elem| result[elem] = default }
result
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L75" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-many-3F">
many?()
</h3>
<div class="description">
<p>Returns <code>true</code> if the enumerable has more than 1 element. Functionally equivalent to <code>enum.to_a.size > 1</code>. Can be called with a block too, much like any?, so <code>people.many? { |p| p.age > 26 }</code> returns <code>true</code> if more than one person is over 26.</p>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 93
def many?
cnt = 0
if block_given?
any? do |*args|
cnt += 1 if yield(*args)
cnt > 1
end
else
any? { (cnt += 1) > 1 }
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L93" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-maximum">
maximum(key)
</h3>
<div class="description">
<p>Calculates the maximum from the extracted elements.</p>
<pre><code>payments = [Payment.new(5), Payment.new(15), Payment.new(10)]
payments.maximum(:price) # => 15
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 40
def maximum(key)
map(&key).max
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L40" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-minimum">
minimum(key)
</h3>
<div class="description">
<p>Calculates the minimum from the extracted elements.</p>
<pre><code>payments = [Payment.new(5), Payment.new(15), Payment.new(10)]
payments.minimum(:price) # => 5
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 32
def minimum(key)
map(&key).min
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L32" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-pick">
pick(*keys)
</h3>
<div class="description">
<p>Extract the given key from the first element in the enumerable.</p>
<pre><code>[{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pick(:name)
# => "David"
[{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pick(:id, :name)
# => [1, "David"]
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 161
def pick(*keys)
return if none?
if keys.many?
keys.map { |key| first[key] }
else
first[keys.first]
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L161" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-pluck">
pluck(*keys)
</h3>
<div class="description">
<p>Extract the given key from each element in the enumerable.</p>
<pre><code>[{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
# => ["David", "Rafael", "Aaron"]
[{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
# => [[1, "David"], [2, "Rafael"]]
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 145
def pluck(*keys)
if keys.many?
map { |element| keys.map { |key| element[key] } }
else
key = keys.first
map { |element| element[key] }
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L145" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-sole">
sole()
</h3>
<div class="description">
<p>Returns the sole item in the enumerable. If there are no items, or more than one item, raises <code>Enumerable::SoleItemExpectedError</code>.</p>
<pre><code>["x"].sole # => "x"
Set.new.sole # => Enumerable::SoleItemExpectedError: no item found
{ a: 1, b: 2 }.sole # => Enumerable::SoleItemExpectedError: multiple items found
</code></pre>
</div>
<details class="method__source">
<summary>
<span class="label">π Source code</span>
</summary>
<pre><code class="ruby"># File activesupport/lib/active_support/core_ext/enumerable.rb, line 211
def sole
case count
when 1 then return first # rubocop:disable Style/RedundantReturn
when 0 then raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "no item found"
when 2.. then raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "multiple items found"
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/enumerable.rb#L211" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-without">
without(*elements)
</h3>
<div class="description">
</div>
<div class="aka">
Alias for: <a href="Enumerable.html#method-i-excluding">excluding</a>
</div>
</div>
</div>
</div>
</div>