You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you may have noticed, Ruby’s standard library comes with a lot of useful solutions (for example, Array#sum).
423
+
424
+
Other quite handy Array methods provided by default are:
425
+
426
+
-`#each_with_index` – use this when you want to iterate over each element and need the index.
427
+
-`#sort` – returns a new array with the elements of the original array, but sorted.
428
+
-`#sort_by` – similar to `#sort`, but allows you to specify the sorting criterion.
429
+
-`#min`, `#max` – returns the minimum/maximum valued element in the array.
430
+
-`#minmax` – returns a new 2-element array containing the minimum and maximum values.
431
+
432
+
This is a short list of the Array’s methods. Be sure to spend some time reading [the Array class documentation](https://ruby-doc.org/current/Array.html); it’s a worthwhile investment of your time.
420
433
421
434
## Key Concepts
422
435
423
436
### Ruby
424
437
425
-
- Array#sum: get the sum the numbers in an array
426
-
- Array#length (aliased as Array#size): get the amount of elements in an array
438
+
- Array#sum: get the sum the numbers in an array.
439
+
- Array#length (aliased as Array#size): get the amount of elements in an array.
440
+
- The default Ruby library has many other useful methods for handling Arrays.
427
441
- Division of integers always result in a truncated integer.
428
442
- Change the divisor to a float to get the result in a float.
0 commit comments