-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.rb
More file actions
174 lines (134 loc) · 6.84 KB
/
arrays.rb
File metadata and controls
174 lines (134 loc) · 6.84 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
=begin
-- arrrays are ordered, interger-indexed lists.
-- arrays can hold any data type but arrays of the same data type are best
practices.
=end
# creating an array
=begin
1. commonest method for creating an array is using the array literal method
array literal is the square brackets []
2. using the Array.new method. The Array.new mthod creates a new empty array.
It also accepts 2 optional arguments. first argument is the size of the array
and the second argument is the default value for all elements in the array.
=end
puts "----------creating an array-----------"
my_array = [1, 2, 3, 4, 5]
p my_array
my_new_array = Array.new(5, 0) # creates an array with 5 elements all set to 0
puts my_new_array
p my_new_array
# accessing array elements
=begin
arrays in ruby are zero-based indexed and -1 less than the array's length.
the first element of the array is 0.
you can access elements of arrau using their index.
array values can also be accessed using negative indices.
-1 returns the last element of the array.
accessing an index that is out of range of the array will return nil.
=end
puts "----------accessing array elements-----------"
strings_test = ["hello", "world", "ruby", "arrays"]
puts strings_test[0] # outputs "hello"
puts strings_test[1] # outputs "world"
puts strings_test[-1] # outputs "arrays"
puts strings_test[-2] # outputs "ruby"
puts strings_test[10] # outputs nil
puts strings_test[-10] # outputs nil
# adding elements to an array
=begin
1. you can add eleemnts to an array using push method or << (shovel operator).
both push and << add new elements to the end of the array and modify the
original array.
push method can take multiple arguments and add them all to the end of the array
<< operator can only add one element at a time.
2. you can also add elements to an array using the unshift method.
the unshift method adds a new element to the beginning of
the array, modifies the original array and returns the new array.
like the push method, unshift can also take multiple arguments and
add them all to the beginning of the array.
3. you can also add elements to an array using the insert method.
the insert method allows you to add elements at a specific index in the array.
it takes two arguments, the first argument is the index where you want to insert
the new element and the second argument is the element you want to insert.
the insert method can also take multiple arguments and insert them all at the
specified index.
4. you can also add elements to an array using the concat method.
the concat method takes an array as the argument and adds all the elements
of the argument array to the end of the original array.
NOTES:
> all the above methods modify the original array and return the modified array.
> the push, unshift & insert methods can take multiple arguments and add them
all to the array. the << operator can only add one element at a time.
> concat, push & << add to end, unshift to the start, insert at a specific index.
=end
puts "----------adding elements to an array-----------"
num_array = [1, 2, 3]
num_array.push(4, 5, 6) # adds 4, 5 and 6 to the end of the array
p num_array # outputs [1, 2, 3, 4, 5, 6]
num_array << 7 # adds 7 to the end of the array
p num_array # outputs [1, 2, 3, 4, 5, 6, 7, 8]
num_array.unshift(-1, 0) # adds -1 and 0 to the beginning of the array
p num_array # outputs [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
num_array.insert(3, "inserted", "another") # inserts "inserted" and "another" at index 3
p num_array # outputs [-1, 0, 1, "inserted", "another", 2, 3, 4, 5, 6, 7, 8]
num_array.concat([9, 10]) # adds 9 and 10 to the end of the array
p num_array # outputs [-1, 0, 1, "inserted", "another", 2, 3, 4, 5, 6, 7, 8, 9, 10]
# removing elements from an array
=begin
1. you can use shift to remove the elements from the start of the array.
shift can take an argument to specify how many elements to remove
from an array. if no argument is given, it removes one element.
shift modifies the original array.
shift returns an array of the removed elements if multiple elements
are removed or just the removed element of the matching data type if
only one element is removed.
2. you can also use pop to remove elements from the end of the array.
pop can also take an argument to specify how many elements to remove
from the end of the array. if no argument is given, it removes one element.
pop modifies the original array and returns an array of the removed
elements if multiple elements are removed or just the removed element
of the matching data type if only one element is removed.
**pop is faster than shift because it does not
have to reindex the remaining elements
3. delete_at method can be used to remove an element at a specific index
in the array. it takes the index of the element to be removed as an
argument, modifies the original array and returns the deleted element.
4. delete method can be used to remove all occurrences of a specific value from
the array. it takes the value to be removed as an argument.
=end
puts "----------removing elements from an array-----------"
str_array = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
shifted_elements = str_array.shift(2) # removes "a", "b" from the array
p str_array # outputs ["c", "d", "e", "f", "g", "h", "i", "j"]
p shifted_elements # outputs ["a", "b"]
popped_elements = str_array.pop(3) # removes "h", "i", "j" from the array
p str_array # outputs ["c", "d", "e", "f", "g"]
p popped_elements # outputs ["h", "i", "j"]
deleted_element = str_array.delete_at(1) # removes "d" from the array
p str_array # outputs ["c", "e", "f", "g"]
puts deleted_element # outputs "d"
str_array = ["c", "e", "f", "e", "g", "e"]
str_array.delete("e") # removes all occurrences of "e" from the array
p str_array # outputs ["c", "f", "g"]
# adding and subtracting arrays
=begin
1. you can add two arrays together using the + operator. this creates a new array
that contains all the elements of both arrays.
the original arrays remain unchanged.
2. subtractng arrays returns an array with all the elements of the first array that
are not present in the second array. the original arrays remain unchanged.
=end
puts "----------adding arrays-----------"
array1 = [1, 2, 3]
array2 = [4, 5, 6]
p array1 + array2 # outputs [1, 2, 3, 4, 5, 6]
p array1 # remains unchanged, outputs [1, 2, 3]
p array2 # remains unchanged, outputs [4, 5, 6]
puts "----------subt arrays-----------"
array3 = [1, 2, 3, 4, 5,6, 7]
array4 = [4, 5, 6]
p array3 - array4 # outputs [1, 2, 3]
p array3 # remains unchanged, outputs [1, 2, 3, 4, 5]
p array4 # remains unchanged, outputs [4, 5, 6]
# iterating over an array
# array methods