Skip to content

Commit 9d3fa23

Browse files
committed
[WIP] Add unit for removing elements from Arrays
1 parent 8281c8e commit 9d3fa23

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
baggage = ['shoes', 'underwear', 'socks', 'kettle', 'glasses', 'shirt', 'wallet']
2+
3+
baggage-shift
4+
baggage.delete('kettle')
5+
baggage.pop
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'rspec'
2+
require 'code_breaker'
3+
4+
describe 'Your code' do
5+
[['solution::code']]
6+
7+
VARIABLE = :baggage
8+
ITEMS = ['glasses', 'shirt', 'trousers', 'socks', 'shoes']
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+
it 'defines a variables "baggage" that has all the items' do
16+
expect(eval(VARIABLE.to_s)).to eq ITEMS
17+
end
18+
19+
if eval(VARIABLE.to_s) == ITEMS
20+
before(:all) do
21+
@statements = CodeBreaker.parse( %q{ [['solution::code']] })
22+
end
23+
24+
it 'uses the unshift method to add "glasses"' do
25+
unshift = [{ lvar: VARIABLE }, :unshift, String]
26+
expect(@statements.include?(unshift)).to be true
27+
end
28+
29+
it 'uses the push or << method to add "shoes"' do
30+
push = [{ lvar: VARIABLE }, :push, String]
31+
arrows = [{ lvar: VARIABLE }, :<<, String]
32+
33+
includes_code = @statements.include?(push) || @statements.include?(arrows)
34+
expect(includes_code).to be true
35+
end
36+
37+
it 'uses the insert method to add "trousers" and "socks"' do
38+
insert = [{ lvar: VARIABLE }, :insert, Fixnum, String, String]
39+
expect(@statements.include?(insert)).to be true
40+
end
41+
end
42+
end
43+
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 removes 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 to remove it will delete 1 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 removed 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` called on an empty Array will return *nil*:
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 the third element.
66+
You can do exactly 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+
Apart from `pop` and `shift`, `drop` does not modify the original Array but creates
73+
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` take one element to remove from the Array as argument and returns the
84+
removed elements:
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 into the 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 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)