Skip to content

Commit f64318c

Browse files
committed
Add unit for accessing Arrays
1 parent 945e039 commit f64318c

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# e.g.:
2+
languages = ['Ruby', 'Elixir', 'Scala', 'Erlang', 'OCaml']
3+
4+
first = languages.first
5+
last = languages.last
6+
7+
some_languages = languages[1..3]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'rspec'
2+
require 'code_breaker'
3+
4+
VARIABLES = [:languages, :first, :last, :some_languages].freeze
5+
6+
describe 'Your code' do
7+
[['solution::code']]
8+
9+
VARIABLES.each do |name|
10+
it "defines a variable with name \"#{name}\"" do
11+
expect(local_variables.include?(name)).to be true
12+
end
13+
14+
if local_variables.include?(name)
15+
if [:languages, :some_languages].include?(name)
16+
it "assigns an Array to the variable \"#{name}\"" do
17+
expect(eval(name.to_s)).to be_an_instance_of Array
18+
end
19+
elsif local_variables.include?(:languages) && name == :first
20+
it "assigns the first language to the variable \"#{name}\"" do
21+
expect(eval(name.to_s)).to eq languages.first
22+
end
23+
elsif local_variables.include?(:languages) && name == :last
24+
it "assigns the last language to the variable \"#{name}\"" do
25+
expect(eval(name.to_s)).to eq languages.last
26+
end
27+
end
28+
29+
if name == :languages
30+
it "assigns an Array to the variable \"#{name}\" with 5 String elements" do
31+
values = Array.new(eval(name.to_s))
32+
expect(values.length).to eq 5
33+
values.each { |value| expect(value).to be_an_instance_of String }
34+
end
35+
end
36+
37+
if local_variables.include?(:languages) && name == :some_languages
38+
it "assigns an Array to the variable \"#{name}\" with 5 String elements" do
39+
expect(eval(name.to_s)).to eq languages[1..3]
40+
end
41+
end
42+
end
43+
end
44+
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Accessing elements
2+
3+
---
4+
5+
*You will learn:*
6+
- how to get single elements from an Array
7+
- how to get subsets of elements from an Array
8+
9+
---
10+
11+
Each element in an Array is associated with a fixed place – its *index*.
12+
The index is an Integer which allows to access an element in the Array.
13+
14+
The index 0 points to the first element, the index 1 to the second element, etc.
15+
Negative indexes point to elements backwards from the last element. E.g. -1 points
16+
to the last element, -2 points to the second last element, and so on.
17+
18+
## Getting a single element from an Array
19+
20+
You can get the object from an Array at an index by using squared brackets:
21+
22+
` numbers = [2, 4, 9]` *# defining an Array `numbers`*
23+
` numbers[0]` *# => 2
24+
` numbers[1]` *# => 4
25+
` numbers[2]` *# => 9
26+
` numbers[-1]` *# => 9
27+
28+
If there is no element present for the given index you will get a *nil* value:
29+
30+
` numbers[3]` *# => nil
31+
32+
`[]` is just an ordinary method that is defined for an instance of Array.
33+
34+
Another way for getting a single element is using the alias method `at`:
35+
36+
` numbers.at(0)` *# => 2
37+
` numbers.at(-1)` *# => 9
38+
` numbers.at(3)` *# => nil
39+
40+
There are two methods for getting special elements of an Array: `first` and `last`.
41+
`first` – as you might have guessed – returns the first element (at index 0),
42+
and `last` returns the last element (at index -1):
43+
44+
` numbers.first` *# => 2*
45+
` numbers.last` *# => 9*
46+
47+
## Getting a subset of elements from an Array
48+
49+
You can also pass 2 parameters to the `[]` method. This allows to select a subarray
50+
by defining the index to start at and the number of elements you want to grab, e.g.:
51+
52+
` numbers[1, 2]` * # => [4, 9]*
53+
54+
returns 2 elements starting at index 1. If there are no elements for the given indexes
55+
available, it returns *nil*:
56+
57+
` numbers[10, 2]` * # => nil*
58+
59+
Another way to select a subarray from an Array is to pass a *Range* of the wanted
60+
indexes to `[]`:
61+
62+
` numbers[1..2]` * # => [4, 9]
63+
64+
In case the elements are not available for the indexes it will again return *nil*:
65+
66+
` numbers[5..10]` * # => nil*
67+
68+
For further information about the `[]` method, see (ruby-doc core: Array#[]).
69+
70+
---
71+
72+
Create an Array of 5 different programming languages (as Strings) and store it in
73+
a *languages* variable.
74+
75+
Define a variable *first* and assign the first programming language to it.
76+
77+
Define a variable *last* and assign the last programming language to it.
78+
79+
Store the middle 3 programming languages in a variable *some_languages*.
80+
81+
---

0 commit comments

Comments
 (0)