-
Notifications
You must be signed in to change notification settings - Fork 1
/
about_arrays.rb
108 lines (84 loc) · 3.28 KB
/
about_arrays.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutArrays < Neo::Koan
def test_creating_arrays
empty_array = Array.new
assert_equal Array, empty_array.class
assert_equal 0, empty_array.size
end
def test_array_literals
array = Array.new
assert_equal [], array
array[0] = 1
assert_equal [1], array
array[1] = 2
assert_equal [1, 2], array
array << 333
assert_equal [1, 2, 333], array
end
# Note:
# "<<" appends an element to the end of an Array
# Array starts from the 0th element
def test_accessing_array_elements
array = [:peanut, :butter, :and, :jelly]
assert_equal :peanut, array[0]
assert_equal :peanut, array.first
assert_equal :jelly, array[3]
assert_equal :jelly, array.last
assert_equal :jelly, array[-1]
assert_equal :butter, array[-3]
end
# Note:
# From RubyDoc:
# Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array---that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.
def test_slicing_arrays
array = [:peanut, :butter, :and, :jelly]
assert_equal [:peanut], array[0,1]
assert_equal [:peanut, :butter], array[0,2]
assert_equal [:and, :jelly], array[2,2]
assert_equal [:and, :jelly], array[2,20]
assert_equal [], array[4,0]
assert_equal [], array[4,100]
assert_equal nil, array[5,0]
end
# Note:
# From RubyDoc:
# Elements in an array can be retrieved using the Array#[] method. It can take a single integer argument (a numeric index), a pair of arguments (start and length) or a range.
def test_arrays_and_ranges
assert_equal Range, (1..5).class
assert_not_equal [1,2,3,4,5], (1..5)
assert_equal [1, 2, 3, 4, 5], (1..5).to_a
assert_equal [1, 2, 3, 4], (1...5).to_a
end
# Note
# From RubyDoc:
# A Range represents an interval—a set of values with a beginning and an end. Ranges may be constructed using the s..e and s...e literals, or with ::new. Ranges constructed using .. run from the beginning to the end inclusively. Those created using ... exclude the end value. When used as an iterator, ranges return each value in the sequence.
def test_slicing_with_ranges
array = [:peanut, :butter, :and, :jelly]
assert_equal [:peanut, :butter, :and], array[0..2]
assert_equal [:peanut, :butter], array[0...2]
assert_equal [:and, :jelly], array[2..-1]
end
def test_pushing_and_popping_arrays
array = [1,2]
array.push(:last)
assert_equal [1, 2, :last], array
popped_value = array.pop
assert_equal :last, popped_value
assert_equal [1, 2], array
end
# Note:
# Push an element to the end of an Array using either << or push()
# pop() removes and returns the last Array element
def test_shifting_arrays
array = [1,2]
array.unshift(:first)
assert_equal [:first, 1, 2], array
shifted_value = array.shift
assert_equal :first, shifted_value
assert_equal [1,2], array
end
# Note
# From RubyDoc:
# shift: Removes the first element and returns it (shifting all other elements down by one). Returns nil if the array is empty.
# unshift: Prepends objects to the front of array, moving other elements upwards.
end