Skip to content

Commit 8281c8e

Browse files
committed
Add unit for adding elements to Arrays
1 parent f64318c commit 8281c8e

3 files changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
baggage = ['shirt']
2+
baggage.unshift('glasses')
3+
baggage.push('shoes')
4+
baggage.insert(2, 'trousers', 'socks')
5+
6+
# baggage
7+
# => ["glasses", "shirt", "trousers", "socks", "shoes"]
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

4_Arrays/3_Adding_elements/task.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Adding elements to Arrays
2+
3+
---
4+
5+
*You will learn:*
6+
- how to add elements to the end of an Array
7+
- how to add elements to the beginning of an Array
8+
- how to add elements at a specific index to an Array
9+
10+
---
11+
12+
If you deal with an Array you will hardly just take the Array as is, but you likely want
13+
to process and modify it or build new Arrays out of it.
14+
15+
This unit will look into different ways of adding elements to an Array.
16+
17+
## Adding elements to the end of an Array
18+
19+
An Array instance has several methods for adding elements.
20+
If you want to add a *single* element to the end you can use the `<<` method:
21+
22+
` numbers = [1, 2, 3]`
23+
` numbers << 4` *# => [1, 2, 3, 4]*
24+
25+
You could also write `numbers.<<(4)`, so the missing *.* and *()* are just a bit of
26+
syntactical sugar.
27+
28+
A similiar method for adding an element to the end of an Array is `push`:
29+
30+
` chars = ['a', 'b']`
31+
` chars.push('c')` *# => ['a', 'b', 'c']*
32+
33+
The difference is, that `push` accepts multiple arguments, so that you can add as
34+
many elements as you want in a single call:
35+
36+
` chars.push('d', 'e', 'f')` *# => ['a', 'b', 'c', 'd', 'e', 'f']*
37+
38+
Both `<<` and `push` will modify the given Array directly. If you check the values
39+
of the variables above after calling the methods on them they will contain the added values:
40+
41+
` numbers` *# => [1, 2, 3, 4] *
42+
` chars` *# => ['a', 'b', 'c', 'd', 'e', 'f']*
43+
44+
If you want to build a new Array out of an existing one by adding elements, the `+` method
45+
will help you to combine two Arrays:
46+
47+
` numbers = [1, 2]`
48+
49+
` more_numbers = numbers + [3]` *# => [1, 2, 3]*
50+
` even_more_numbers = numbers + [-1, -2]` *# => [1, 2, -1, -2]*
51+
52+
The original Array will stay the same:
53+
54+
` numbers` *# => [1, 2]*
55+
56+
## Adding elements to the beginning of an Array
57+
58+
If you want to add elements to the beginning of an Array instead, you can use the
59+
`unshift` method which takes 1 or more elements to add as arguments:
60+
61+
` numbers = [1, 2]`
62+
` numbers.unshift(0)` *# => [0, 1, 2]*
63+
` numbers.unshift(-2, -1)` *# => [-2, -1, 0, 1, 2]*
64+
65+
Similar to `push`, the `unshift` method will modify the original Array.
66+
67+
## Addding elements at a certain index
68+
69+
If adding an element to the end or the beginning of an Array is not what you want,
70+
then there is an `insert` method, that allows you to insert one or more elements
71+
at a defined position:
72+
73+
` vegetables = ['potato', 'pepper']`
74+
` vegetables.insert(1, 'cucumber', 'cauliflower')`
75+
76+
The first argument is the index where to insert the first element (keep in mind, that
77+
the indexing starts at 0, so *1* means: “Insert before the second element”).
78+
79+
Like `<<` and `push`, `insert` also modifies the original Array, so that *vegetables* holds
80+
all the values now:
81+
82+
` vegetables` *# => ['potato', 'cucumber', 'cauliflower', 'pepper']
83+
84+
Now that you have learned a couple of possibilities for adding elements to an Array
85+
let’s summarize them again:
86+
87+
` <<` *adds element at end modifies the original Array*
88+
` push` *adds elements at end modifies the original Array*
89+
` +` *adds elements at end creates a new Array*
90+
` unshift` *adds elements at beginning modifies the original Array*
91+
` insert` *adds elements at given index modifies the original Array*
92+
93+
---
94+
95+
Prepare a case with some things for a trip:
96+
97+
Create a variable *baggage* holding an Array with one element *"shirt"*.
98+
Add a pair of *"glasses"* before the shirt and some *"shoes"* after the shirt.
99+
Then add another *"trousers"* and a pair of *"socks"* before the shoes.
100+
101+
---

0 commit comments

Comments
 (0)