/
ex32.rb
38 lines (29 loc) · 1.01 KB
/
ex32.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
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# this first kind of for-Loop goes through an array
for number in the_count
puts "This is count #{number}"
end
# same as above, but using a block instead
fruits.each do |fruit|
puts "A fruit of type: #{fruit}"
end
# also we can go though mixed arrays too
for i in change
puts "I got #{i}"
end
# we can also build arrays, first start with an empty one
elements = []
# then use a range object to do 0 to 5 counts
# EC: It looks like elements.push(0..5) adds a value of "0..5" to the first element of the elements array, so you need to actually use a for loop to populate it, or maybe 0..5.times?
for i in (0..5)
puts "Adding #{i} to the list."
# push is a function that arrays understand
elements.push(i)
end
# now we can puts them out too
for i in elements
puts "Element was: #{i}"
end
# EC: For arrays, removal methods include shift and pop, and additive methods include unshift and push.