-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArray.html
1518 lines (917 loc) Β· 45.2 KB
/
Array.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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: Array
layout: default
---
<div class="main">
<div class="banner">
<span>Ruby on Rails 8.0.0</span><br />
<div class="type">Class</div>
<h1>
Array
<span class="parent"><
<a href="Object.html">Object</a>
</span>
</h1>
<ul class="files">
<li><a href="../files/activesupport/lib/active_support/core_ext/array/access_rb.html">activesupport/lib/active_support/core_ext/array/access.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/array/conversions_rb.html">activesupport/lib/active_support/core_ext/array/conversions.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/array/extract_rb.html">activesupport/lib/active_support/core_ext/array/extract.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/array/extract_options_rb.html">activesupport/lib/active_support/core_ext/array/extract_options.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/array/grouping_rb.html">activesupport/lib/active_support/core_ext/array/grouping.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/array/inquiry_rb.html">activesupport/lib/active_support/core_ext/array/inquiry.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/array/wrap_rb.html">activesupport/lib/active_support/core_ext/array/wrap.rb</a></li>
<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/blank_rb.html">activesupport/lib/active_support/core_ext/object/blank.rb</a></li>
<li><a href="../files/activesupport/lib/active_support/core_ext/object/deep_dup_rb.html">activesupport/lib/active_support/core_ext/object/deep_dup.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>
<li><a href="../files/activesupport/lib/active_support/core_ext/object/to_query_rb.html">activesupport/lib/active_support/core_ext/object/to_query.rb</a></li>
</ul>
</div>
<div id="bodyContent">
<div id="content">
<h2 id="methods">Methods</h2>
<ul>
<li>
<a href="#method-i-deep_dup">deep_dup</a>
</li>
<li>
<a href="#method-i-excluding">excluding</a>
</li>
<li>
<a href="#method-i-extract-21">extract!</a>
</li>
<li>
<a href="#method-i-extract_options-21">extract_options!</a>
</li>
<li>
<a href="#method-i-fifth">fifth</a>
</li>
<li>
<a href="#method-i-forty_two">forty_two</a>
</li>
<li>
<a href="#method-i-fourth">fourth</a>
</li>
<li>
<a href="#method-i-from">from</a>
</li>
<li>
<a href="#method-i-in_groups">in_groups</a>
</li>
<li>
<a href="#method-i-in_groups_of">in_groups_of</a>
</li>
<li>
<a href="#method-i-including">including</a>
</li>
<li>
<a href="#method-i-inquiry">inquiry</a>
</li>
<li>
<a href="#method-i-second">second</a>
</li>
<li>
<a href="#method-i-second_to_last">second_to_last</a>
</li>
<li>
<a href="#method-i-split">split</a>
</li>
<li>
<a href="#method-i-third">third</a>
</li>
<li>
<a href="#method-i-third_to_last">third_to_last</a>
</li>
<li>
<a href="#method-i-to">to</a>
</li>
<li>
<a href="#method-i-to_formatted_s">to_formatted_s</a>
</li>
<li>
<a href="#method-i-to_fs">to_fs</a>
</li>
<li>
<a href="#method-i-to_param">to_param</a>
</li>
<li>
<a href="#method-i-to_query">to_query</a>
</li>
<li>
<a href="#method-i-to_sentence">to_sentence</a>
</li>
<li>
<a href="#method-i-to_xml">to_xml</a>
</li>
<li>
<a href="#method-i-without">without</a>
</li>
<li>
<a href="#method-c-wrap">wrap</a>
</li>
</ul>
<!-- Methods -->
<h2 id="class-public-methods">Class Public methods</h2>
<div class="method">
<h3 id="method-c-wrap">
wrap(object)
</h3>
<div class="description">
<p>Wraps its argument in an array unless it is already an array (or array-like).</p>
<p>Specifically:</p>
<ul><li>
<p>If the argument is <code>nil</code> an empty array is returned.</p>
</li><li>
<p>Otherwise, if the argument responds to <code>to_ary</code> it is invoked, and its result returned.</p>
</li><li>
<p>Otherwise, returns an array with the argument as its single element.</p>
<pre><code>Array.wrap(nil) # => []
Array.wrap([1, 2, 3]) # => [1, 2, 3]
Array.wrap(0) # => [0]
</code></pre>
</li></ul>
<p>This method is similar in purpose to <code>Kernel#Array</code>, but there are some differences:</p>
<ul><li>
<p>If the argument responds to <code>to_ary</code> the method is invoked. <code>Kernel#Array</code> moves on to try <code>to_a</code> if the returned value is <code>nil</code>, but <code>Array.wrap</code> returns an array with the argument as its single element right away.</p>
</li><li>
<p>If the returned value from <code>to_ary</code> is neither <code>nil</code> nor an <code>Array</code> object, <code>Kernel#Array</code> raises an exception, while <code>Array.wrap</code> does not, it just returns the value.</p>
</li><li>
<p>It does not call <code>to_a</code> on the argument, if the argument does not respond to <code>to_ary</code> it returns an array with the argument as its single element.</p>
</li></ul>
<p>The last point is easily explained with some enumerables:</p>
<pre><code>Array(foo: :bar) # => [[:foo, :bar]]
Array.wrap(foo: :bar) # => [{:foo=>:bar}]
</code></pre>
<p>Thereβs also a related idiom that uses the splat operator:</p>
<pre><code>[*object]
</code></pre>
<p>which returns <code>[]</code> for <code>nil</code>, but calls to <code>Array(object)</code> otherwise.</p>
<p>The differences with <code>Kernel#Array</code> explained above apply to the rest of <code>object</code>s.</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/array/wrap.rb, line 39
def self.wrap(object)
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary || [object]
else
[object]
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/wrap.rb#L39" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<h2 id="instance-public-methods">Instance Public methods</h2>
<div class="method">
<h3 id="method-i-deep_dup">
deep_dup()
</h3>
<div class="description">
<p>Returns a deep copy of array.</p>
<pre><code>array = [1, [2, 3]]
dup = array.deep_dup
dup[1][2] = 4
array[1][2] # => nil
dup[1][2] # => 4
</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/object/deep_dup.rb, line 29
def deep_dup
map(&:deep_dup)
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/object/deep_dup.rb#L29" 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 <a href="Array.html"><code>Array</code></a> excluding the specified elements.</p>
<pre><code>["David", "Rafael", "Aaron", "Todd"].excluding("Aaron", "Todd") # => ["David", "Rafael"]
[ [ 0, 1 ], [ 1, 0 ] ].excluding([ [ 1, 0 ] ]) # => [ [ 0, 1 ] ]
</code></pre>
<p>Note: This is an optimization of <code>Enumerable#excluding</code> that uses <code>Array#-</code> instead of <code>Array#reject</code> for performance reasons.</p>
</div>
<div class="aka">
Also aliased as: <a href="Array.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/array/access.rb, line 47
def excluding(*elements)
self - elements.flatten(1)
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L47" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-extract-21">
extract!()
</h3>
<div class="description">
<p>Removes and returns the elements for which the block returns a true value. If no block is given, an Enumerator is returned instead.</p>
<pre><code>numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9]
numbers # => [0, 2, 4, 6, 8]
</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/array/extract.rb, line 10
def extract!
return to_enum(:extract!) { size } unless block_given?
extracted_elements = []
reject! do |element|
extracted_elements << element if yield(element)
end
extracted_elements
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/extract.rb#L10" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-extract_options-21">
extract_options!()
</h3>
<div class="description">
<p>Extracts options from a set of arguments. Removes and returns the last element in the array if itβs a hash, otherwise returns a blank hash.</p>
<pre><code>def options(*args)
args.extract_options!
end
options(1, 2) # => {}
options(1, 2, a: :b) # => {:a=>:b}
</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/array/extract_options.rb, line 24
def extract_options!
if last.is_a?(Hash) && last.extractable_options?
pop
else
{}
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/extract_options.rb#L24" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-fifth">
fifth()
</h3>
<div class="description">
<p>Equal to <code>self[4]</code>.</p>
<pre><code>%w( a b c d e ).fifth # => "e"
</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/array/access.rb, line 76
def fifth
self[4]
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L76" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-forty_two">
forty_two()
</h3>
<div class="description">
<p>Equal to <code>self[41]</code>. Also known as accessing βthe redditβ.</p>
<pre><code>(1..42).to_a.forty_two # => 42
</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/array/access.rb, line 83
def forty_two
self[41]
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L83" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-fourth">
fourth()
</h3>
<div class="description">
<p>Equal to <code>self[3]</code>.</p>
<pre><code>%w( a b c d e ).fourth # => "d"
</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/array/access.rb, line 69
def fourth
self[3]
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L69" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-from">
from(position)
</h3>
<div class="description">
<p>Returns the tail of the array from <code>position</code>.</p>
<pre><code>%w( a b c d ).from(0) # => ["a", "b", "c", "d"]
%w( a b c d ).from(2) # => ["c", "d"]
%w( a b c d ).from(10) # => []
%w().from(0) # => []
%w( a b c d ).from(-2) # => ["c", "d"]
%w( a b c ).from(-10) # => []
</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/array/access.rb, line 12
def from(position)
self[position, length] || []
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L12" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-in_groups">
in_groups(number, fill_with = nil, &block)
</h3>
<div class="description">
<p>Splits or iterates over the array in <code>number</code> of groups, padding any remaining slots with <code>fill_with</code> unless it is <code>false</code>.</p>
<pre><code>%w(1 2 3 4 5 6 7 8 9 10).in_groups(3) {|group| p group}
["1", "2", "3", "4"]
["5", "6", "7", nil]
["8", "9", "10", nil]
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, '&nbsp;') {|group| p group}
["1", "2", "3", "4"]
["5", "6", "7", "&nbsp;"]
["8", "9", "10", "&nbsp;"]
%w(1 2 3 4 5 6 7).in_groups(3, false) {|group| p group}
["1", "2", "3"]
["4", "5"]
["6", "7"]
</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/array/grouping.rb, line 62
def in_groups(number, fill_with = nil, &block)
# size.div number gives minor group size;
# size % number gives how many objects need extra accommodation;
# each group hold either division or division + 1 items.
division = size.div number
modulo = size % number
# create a new array avoiding dup
groups = []
start = 0
number.times do |index|
length = division + (modulo > 0 && modulo > index ? 1 : 0)
groups << last_group = slice(start, length)
last_group << fill_with if fill_with != false &&
modulo > 0 && length == division
start += length
end
if block_given?
groups.each(&block)
else
groups
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/grouping.rb#L62" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-in_groups_of">
in_groups_of(number, fill_with = nil, &block)
</h3>
<div class="description">
<p>Splits or iterates over the array in groups of size <code>number</code>, padding any remaining slots with <code>fill_with</code> unless it is <code>false</code>.</p>
<pre><code>%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3) {|group| p group}
["1", "2", "3"]
["4", "5", "6"]
["7", "8", "9"]
["10", nil, nil]
%w(1 2 3 4 5).in_groups_of(2, '&nbsp;') {|group| p group}
["1", "2"]
["3", "4"]
["5", "&nbsp;"]
%w(1 2 3 4 5).in_groups_of(2, false) {|group| p group}
["1", "2"]
["3", "4"]
["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/array/grouping.rb, line 22
def in_groups_of(number, fill_with = nil, &block)
if number.to_i <= 0
raise ArgumentError,
"Group size must be a positive integer, was #{number.inspect}"
end
if fill_with == false
collection = self
else
# size % number gives how many extra we have;
# subtracting from number gives how many to add;
# modulo number ensures we don't add group of just fill.
padding = (number - size % number) % number
collection = dup.concat(Array.new(padding, fill_with))
end
if block_given?
collection.each_slice(number, &block)
else
collection.each_slice(number).to_a
end
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/grouping.rb#L22" 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 ]
[ [ 0, 1 ] ].including([ [ 1, 0 ] ]) # => [ [ 0, 1 ], [ 1, 0 ] ]
</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/array/access.rb, line 36
def including(*elements)
self + elements.flatten(1)
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L36" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-inquiry">
inquiry()
</h3>
<div class="description">
<p>Wraps the array in an <a href="ActiveSupport/ArrayInquirer.html"><code>ActiveSupport::ArrayInquirer</code></a> object, which gives a friendlier way to check its string-like contents.</p>
<pre><code>pets = [:cat, :dog].inquiry
pets.cat? # => true
pets.ferret? # => false
pets.any?(:cat, :ferret) # => true
pets.any?(:ferret, :alligator) # => false
</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/array/inquiry.rb, line 16
def inquiry
ActiveSupport::ArrayInquirer.new(self)
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/inquiry.rb#L16" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-second">
second()
</h3>
<div class="description">
<p>Equal to <code>self[1]</code>.</p>
<pre><code>%w( a b c d e ).second # => "b"
</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/array/access.rb, line 55
def second
self[1]
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L55" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-second_to_last">
second_to_last()
</h3>
<div class="description">
<p>Equal to <code>self[-2]</code>.</p>
<pre><code>%w( a b c d e ).second_to_last # => "d"
</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/array/access.rb, line 97
def second_to_last
self[-2]
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L97" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-split">
split(value = nil, &block)
</h3>
<div class="description">
<p>Divides the array into one or more subarrays based on a delimiting <code>value</code> or the result of an optional block.</p>
<pre><code>[1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
(1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
</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/array/grouping.rb, line 93
def split(value = nil, &block)
arr = dup
result = []
if block_given?
while (idx = arr.index(&block))
result << arr.shift(idx)
arr.shift
end
else
while (idx = arr.index(value))
result << arr.shift(idx)
arr.shift
end
end
result << arr
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/grouping.rb#L93" target="_blank" class="github_url">π See on GitHub</a>
</details>
</div>
<div class="method">
<h3 id="method-i-third">
third()
</h3>
<div class="description">
<p>Equal to <code>self[2]</code>.</p>
<pre><code>%w( a b c d e ).third # => "c"
</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/array/access.rb, line 62
def third
self[2]
end</code></pre>
<a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activesupport/lib/active_support/core_ext/array/access.rb#L62" target="_blank" class="github_url">π See on GitHub</a>
</details>