Skip to content

Commit 945e039

Browse files
committed
Add Arrays chapter and Creating Arrays unit
1 parent 573ca32 commit 945e039

3 files changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# e.g.:
2+
objects = [2.75, true, []]
3+
question_marks = Array.new(3, '?')
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
require 'rspec'
2+
require 'rspec/expectations'
3+
require 'code_breaker'
4+
5+
VARIABLES = [:objects, :question_marks].freeze
6+
7+
def code_lines
8+
%q{[['solution::code']]}
9+
end
10+
11+
def statements
12+
@statements ||= Array(CodeBreaker.parse(code_lines))
13+
end
14+
15+
def assignment?(statement)
16+
statement.is_a?(Hash) && statement.keys.first == :lvasgn
17+
end
18+
19+
def brackets_statement
20+
@brackests_statement ||= statements.select do |statement|
21+
assignment?(statement) && statement[:lvasgn].first == VARIABLES.first
22+
end
23+
end
24+
25+
def new_statement
26+
@new_statement ||= statements.select do |statement|
27+
assignment?(statement) && statement[:lvasgn].first == VARIABLES.last
28+
end
29+
end
30+
31+
def array_assigned?(variable)
32+
!!defined?(variable) && variable.is_a?(Array)
33+
end
34+
35+
def array_defined_with_new?(variable, statement)
36+
array_assigned?(variable) &&
37+
statement.first[:lvasgn][1][0..1] == [{ const: :Array }, :new]
38+
end
39+
40+
describe 'Your code' do
41+
[['solution::code']]
42+
43+
VARIABLES.each do |name|
44+
it "defines a variable with name \"#{name}\"" do
45+
expect(local_variables.include?(name)).to be true
46+
end
47+
48+
if local_variables.include?(name)
49+
it "assigns an Array to the variable \"#{name}\"" do
50+
expect(eval(name.to_s)).to be_an Array
51+
end
52+
end
53+
end
54+
55+
if (local_variables & VARIABLES).sort == VARIABLES.sort
56+
describe 'assings an Array to "objects" which' do
57+
it 'is created by using []' do
58+
expect(array_assigned?(objects)).to be true
59+
expect(brackets_statement.first[:lvasgn].last.keys.first).to eq :array
60+
end
61+
62+
if array_assigned?(objects)
63+
before { @values = brackets_statement.first[:lvasgn].last[:array] }
64+
65+
it 'holds a Float' do
66+
expect(@values.include?(Float)).to be true
67+
end
68+
69+
it 'holdd a Boolean' do
70+
included = @values.include?(TrueClass) || @values.include?(FalseClass)
71+
expect(included).to be true
72+
end
73+
74+
it 'holds an empty Array' do
75+
empty_array = { array: [] }
76+
expect(@values.include?(empty_array)).to be true
77+
end
78+
end
79+
end
80+
81+
describe 'assings an Array to "question_marks" which' do
82+
it 'is created by using #new' do
83+
expect(array_defined_with_new?(question_marks, new_statement)).to be true
84+
end
85+
86+
if array_defined_with_new?(question_marks, new_statement)
87+
it 'holds three times "?"' do
88+
expect(question_marks).to eq ['?'] * 3
89+
end
90+
end
91+
end
92+
end
93+
end

4_Arrays/1_Creating_Arrays/task.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Creating Arrays
2+
3+
---
4+
5+
*You will learn:*
6+
- what an Array is
7+
- what you can store in an Array
8+
- how to create Arrays
9+
10+
---
11+
12+
An Array is a *collection of objects*.
13+
14+
This collection is *ordered*, which means that each object in an Array has a fixed
15+
place assigned, a so called *index*.
16+
The index is defined by an integer starting with 0: The first object in the Array
17+
has the index 0, the second has the index 1, etc.
18+
19+
The index can be used to access the objects in an Array. You will learn more
20+
about using the index in the next unit "Accessing elements".
21+
22+
An Array can hold all kinds of Ruby objects, such as Strings, Numbers, Booleans,
23+
other Arrays, etc. The objects in an Array don‘t need to be of the same type:
24+
An Array can hold all kinds of different objects (e.g. Strings, and Numbers) at the same time.
25+
26+
Let‘s create some Arrays!
27+
28+
## Creating an Array with []
29+
30+
Arrays can be created in different ways.
31+
The shortest is surrounding a comma-separated list of objects with square brackets:
32+
33+
` [1, 'Mary', true]` *# => [1, "Mary", true]*
34+
35+
An Array is a Ruby object itself. If you ask for an Array‘s class with the `class` method:
36+
37+
` numbers = [1, 2, 3]`
38+
` numbers.class` *# => Array*
39+
40+
you will get `Array`. An Array is an object of type *Array*.
41+
42+
Instead of using `[]` you can also use the the longer way and call the `[]` method
43+
of the *Array* class:
44+
45+
` Array[1, 2, 3]` *# => [1, 2, 3]*
46+
47+
## Creating an Array with #new
48+
49+
Besides using `[]`, you can create an Array by instanciating an Array with the `new` method.
50+
The `new` method accepts 2 optional parameters: The number of elements, and the element itself.
51+
52+
If you call `new` without any parameter it will create an empty Array:
53+
54+
` Array.new` * # => []*
55+
56+
If you only define the first parameter as an Integer, it will use this Integer to define
57+
the length of the new Array and will assign *nil* for each element:
58+
59+
` Array.new(2)` * # => [nil, nil]
60+
61+
If you pass an Array as first parameter it will return the defined Array:
62+
63+
` Array.new([1, 2, 3])` * # => [1, 2, 3]*
64+
65+
The actual interesting case is, if you pass both parameters.
66+
It will create an array of the length given in the first parameter, with each element
67+
being the second parameter. Here is an example:
68+
69+
` Array.new(3, 4.5)` * # => [4.5, 4.5, 4.5]*
70+
` Array.new(2, ['a', 'b'])` * # => [['a', 'b'], ['a', 'b']]
71+
72+
73+
Another way of creating an Array is calling `Array()` with 1 parameter:
74+
75+
` Array('banana')` * # => ["banana"]*
76+
77+
This will create a new Array with only the given value and is the same as calling `['banana']`.
78+
79+
---
80+
81+
Assign an Array to a variable *objects* by using the square brackets syntax for creating the Array.
82+
The Array should have 3 elements: a Float, a Boolean, and an empty Array.
83+
84+
Create an Array that holds 3 times the String *'?'* by using the `new` method.
85+
Assign the Array to a variable *question_marks*.
86+
87+
---

0 commit comments

Comments
 (0)