Skip to content

Commit 153f118

Browse files
committed
Add unit for removing elements from Array
1 parent 8281c8e commit 153f118

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
baggage = ['shoes', 'underwear', 'socks', 'kettle', 'glasses', 'shirt', 'wallet']
2+
3+
baggage.shift
4+
baggage.delete('kettle')
5+
baggage.pop
6+
7+
# baggage
8+
# => ["underwear", "socks", "glasses", "shirt"]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'rspec'
2+
require 'code_breaker'
3+
4+
describe 'Your code' do
5+
[['solution::code']]
6+
7+
VARIABLE = :baggage
8+
ITEMS = ['shoes', 'underwear', 'socks', 'kettle', 'glasses', 'shirt', 'wallet']
9+
10+
it 'defines a variable with name "baggage"' do
11+
expect(local_variables.include?(VARIABLE)).to be true
12+
end
13+
14+
if local_variables.include?(VARIABLE)
15+
before(:all) do
16+
@statements = CodeBreaker.parse(%q{ [['solution::code']] })
17+
end
18+
19+
it 'uses the shift method to remove "shoes"' do
20+
shift = [{ lvar: VARIABLE }, :shift]
21+
expect(@statements.include?(shift)).to be true
22+
end
23+
24+
it 'uses the delete method to remove the "kettle"' do
25+
delete = [{ lvar: VARIABLE }, :delete, String]
26+
expect(@statements.include?(delete)).to be true
27+
end
28+
29+
it 'uses the pop method to remove the "wallet"' do
30+
pop = [{ lvar: VARIABLE }, :pop]
31+
expect(@statements.include?(pop)).to be true
32+
end
33+
34+
it 'changes your baggage to only contain "underwear", "socks", "glasses", & "shirt"' do
35+
expect(eval(VARIABLE.to_s)).to eq ['underwear', 'socks', 'glasses', 'shirt']
36+
end
37+
end
38+
end
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Removing elements from Arrays
2+
3+
---
4+
5+
*You will learn:*
6+
- how to remove elements from the end of an Array
7+
- how to remove elements from the beginning of an Array
8+
- how to remove elements from an Array after a specific index
9+
- how to remove a specific element
10+
11+
---
12+
13+
Once you have created an Array, there might be the case that you don’t want to keep
14+
all of the Array’s elements. This is where some methods for removing elements come
15+
in handy.
16+
17+
This unit will look into different ways of removing elements from an Array.
18+
19+
## Removing elements from the end of an Array
20+
21+
For removing one ore more elements from the end of an Array you can use the `pop`
22+
method. The `pop` method modifies the orginal Array and returns the removed elements.
23+
You can pass the number of elements to remove as argument:
24+
25+
` numbers = [1, 2, 3, 4]`
26+
` numbers.pop(2)` *# => [3, 4]*
27+
` numbers` *# => [1, 2]*
28+
29+
If you don’t pass the number of elements it will delete one element by default:
30+
31+
` numbers = [5, 6, 7, 8]`
32+
` numbers.pop` *# => 8*
33+
` numbers` *# => [5, 6, 7]*
34+
35+
Note that, if you remove multiple elements, `pop` will return an Array with the removed
36+
elements. If you just remove the last element it will return this element.
37+
38+
If you try to pop an element from an empty Array, there’s no element left to remove
39+
and it will return *nil*:
40+
41+
` [].pop` *# => nil*
42+
43+
## Removing elements from the beginning of an Array
44+
45+
For removing elements from the beginning of an Array you can use the `shift` method.
46+
It takes an optional argument that specifies the number of elements to remove:
47+
48+
` numbers = [0, 1, 2, 3]`
49+
` numbers.shift(3)` *# => [2, 3]*
50+
` numbers` *# => [0, 1]*
51+
52+
If you don’t pass an argument it will only remove and return the very first element:
53+
54+
` numbers = [2, 4, 6]`
55+
` numbers.shift` *# => 2*
56+
` numbers` *# => [4, 6]*
57+
58+
Similar to `pop`, `shift` will return *nil* if it is called on an empty Array:
59+
60+
` [].shift` *# => nil*
61+
62+
## Removing elements after a specific index
63+
64+
There might be situations where you want to select all elements of an Array after
65+
a certain position, e.g. all elements after the third element.
66+
You can do this with the `drop` method:
67+
68+
` fruits = ['apple', 'pear', 'lemon', 'melon']`
69+
` fruits.drop(2)` *# => ["lemon", "melon"]*
70+
` fruits` *# => ["apple", "pear", "lemon", "melon"]*
71+
72+
In contrast to `pop` and `shift`, `drop` does not modify the original Array but
73+
creates a new Array that comprises the dropped elements.
74+
75+
## Removing specific elements
76+
77+
In case you know the elements in an Array, you can specificly remove these.
78+
There are two similar methods for doing this: `delete` and `-`.
79+
80+
The difference is, that `delete` modifies the original Array, while `-` creates a new
81+
Array and does not change the original Array.
82+
83+
`delete` takes the element that should be removed from the Array and returns the
84+
removed element:
85+
86+
` fruits = ['apple', 'lemon', 'lemon']`
87+
88+
` fruits.delete('lemon')` *# => "lemon"*
89+
` fruits` *# => ["apple"]*
90+
91+
As you can see, `delete` removes duplicated elements from the Array, here all the
92+
*'lemon'* elements.
93+
94+
The `-` method takes an Array as argument. It also removes duplicated elements:
95+
96+
` fruits = ['apple', 'lemon', 'lemon', 'melon']`
97+
98+
` less_fruits = fruits - ['lemon']` *# => ["apple", "melon"]*
99+
` fruits` *# => ["apple", "lemon", "lemon", "melon"]*
100+
101+
The `-` does not modify the original Array but creates a new one. The original
102+
Array that is stored in *fruits* remains the same.
103+
104+
Before going to this unit‘s task, let’s summarize all the learned methods for removing
105+
elements from an Array:
106+
107+
` pop` *removes elements at end modifies the original Array*
108+
` shift` *removes elements at beginning modifies the original Array*
109+
` drop` *remove elements after given index creates a new Array*
110+
` delete` *removes the given element modifies the original Array*
111+
` -` *removes the given elements creates a new Array*
112+
113+
---
114+
115+
Let’s use our baggage again and unpack a couple of things.
116+
Define a variable *baggage* and assign an array with all the things you want to
117+
take with you: *shoes*, *underwear*, *socks*, *kettle*, *glasses*, *shirt*, and *wallet*.
118+
119+
You just realized, that you actually want to wear your *shoes* (which are the first
120+
thing in your baggage), so remove them from your *baggage*.
121+
122+
Your friend tells you that you won’t need the *kettle*, so delete it from your *baggage*, too.
123+
124+
Last, you think that it’s maybe a better idea to carry your *wallet* with you.
125+
Thus, also pop the wallet from your *baggage*.
126+
127+
Now you’re good to go. Bon voyage!
128+
129+
---

0 commit comments

Comments
 (0)