Improvements to Common Array Methods Ruby guide (#24705)
- Edits to the intro paragraph for shorter and clearer sentences. - Updated the `.length` description to refer to it as *the* array rather than *your*, to be consistent with other descriptions. - For `.first` and `.last`, I updated the wording from "accesses" to "returns". - Added clarification that `.pop` returns the removed element (since `.shift` also mentions this). - Updated the code snippet for `.select`, which mistakenly implied that the original array is modified - Added clarification that `.include?` returns `true` if the array includes the element. - The section for `.flatten` was stuck in the code snippet for `include?`, I've fixed that. - Added clarification that `.uniq` does not modify the original array. - Updated the `.uniq` description by cutting extra wording. `.uniq` doesn't take an array, it's called on an array.
This commit is contained in:
@ -3,7 +3,7 @@ title: Common Array Methods
|
||||
---
|
||||
## Common Array Methods
|
||||
|
||||
Ruby Arrays form a core foundation in programming in Ruby, and most languages in fact. It is used so much that it would be beneficial to know and even memorize some of the most commonly used methods for arrays. If you want to know more about Ruby Arrays, we have [an article about them](https://guide.freecodecamp.org/ruby/ruby-arrays).
|
||||
Arrays are a core foundation of programming in Ruby and most languages. Arrays are so common that it is beneficial to know, and even memorize, some of their most commonly used methods. If you want to know more about Ruby Arrays, we have [an article about them](https://guide.freecodecamp.org/ruby/ruby-arrays).
|
||||
|
||||
For the purpose of this guide, our array will be as follows:
|
||||
|
||||
@ -12,7 +12,7 @@ array = [0, 1, 2, 3, 4]
|
||||
```
|
||||
|
||||
#### .length
|
||||
The .length method tallies the number of elements in your array and returns the count:
|
||||
The .length method tallies the number of elements in the array and returns the count:
|
||||
|
||||
``` ruby
|
||||
array.length
|
||||
@ -23,14 +23,13 @@ This is also similar to .count and .size methods.
|
||||
``` ruby
|
||||
array.count
|
||||
=> 5
|
||||
```
|
||||
``` ruby
|
||||
|
||||
array.size
|
||||
=> 5
|
||||
```
|
||||
|
||||
#### .first
|
||||
The .first method accesses the first element of the array, the element at index 0:
|
||||
The .first method returns the first element of the array, the element at index 0:
|
||||
|
||||
``` ruby
|
||||
array.first
|
||||
@ -38,7 +37,7 @@ array.first
|
||||
```
|
||||
|
||||
#### .last
|
||||
The .last method accesses the last element of the array:
|
||||
The .last method returns the last element of the array:
|
||||
|
||||
``` ruby
|
||||
array.last
|
||||
@ -62,7 +61,7 @@ array.drop(3)
|
||||
```
|
||||
|
||||
#### array index
|
||||
You can access a specific element in an array by accessing its index. If the index does not exist in the array, nil will be returned:
|
||||
You can return a specific element in an array by accessing its index. If the index does not exist in the array, `nil` will be returned:
|
||||
|
||||
```ruby
|
||||
array[2]
|
||||
@ -91,14 +90,15 @@ array
|
||||
```
|
||||
|
||||
#### .push
|
||||
The .push method will allow you to add an element to the end of an array:
|
||||
The .push method will add an element to the end of an array:
|
||||
|
||||
``` ruby
|
||||
array.push(99)
|
||||
=> [0, 1, 2, 3, 4, 99]
|
||||
```
|
||||
|
||||
#### .unshift
|
||||
The .unshift method will allow you to add an element to the beginning of an array:
|
||||
The .unshift method adds an element to the beginning of an array:
|
||||
|
||||
```
|
||||
array = [2, 3]
|
||||
@ -115,7 +115,7 @@ array.delete(1)
|
||||
```
|
||||
|
||||
#### .delete_at
|
||||
The .delete_at method allows you to permanently remove an element of an array at a specified index:
|
||||
The .delete_at method permanently removes an element of an array at a specified index:
|
||||
|
||||
``` ruby
|
||||
array.delete_at(0)
|
||||
@ -129,6 +129,7 @@ The .reverse method reverses the array but does not mutate it (the original arra
|
||||
array.reverse
|
||||
=> [4, 3, 2, 1, 0]
|
||||
```
|
||||
|
||||
#### .select
|
||||
The .select method iterates over an array and returns a new array that includes any items that return true to the expression provided.
|
||||
|
||||
@ -136,12 +137,10 @@ The .select method iterates over an array and returns a new array that includes
|
||||
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
array.select { |number| number > 4 }
|
||||
=> [5, 6, 7, 8, 9, 10]
|
||||
array
|
||||
=> [5, 6, 7, 8, 9, 10]
|
||||
```
|
||||
|
||||
#### .include?
|
||||
The include? method checks to see if the argument given is included in the array:
|
||||
The include? method checks to see if the argument given is included in the array and returns `true` if it is:
|
||||
|
||||
``` ruby
|
||||
array = [1, 2, 3, 4, 5]
|
||||
@ -200,7 +199,7 @@ end
|
||||
```
|
||||
|
||||
#### .uniq
|
||||
The .uniq method takes in an array containing duplicate elements, and returns a copy of the array containing only unique elements--any duplicate elements are removed from the array.
|
||||
The .uniq method returns a copy of the array containing only unique elements--any duplicate elements are removed from the array. The original array is not modified.
|
||||
|
||||
``` ruby
|
||||
array = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8]
|
||||
|
Reference in New Issue
Block a user