-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex32.rb
executable file
·42 lines (33 loc) · 924 Bytes
/
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
38
39
40
41
#!/usr/bin/env ruby
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, iterates over, an array
for number in the_count
puts "This is the count #{number}"
end
# Same as above but using a block instaed
fruits.each do |fruit|
puts "A fruit of type: #{fruit}"
end
# We can also interate over mixed arrays
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
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 the elements in the "elements" array
for i in elements
puts "Element was: #{i}"
end
# Let's try the above with a block instead
elements.each do |element|
puts "Element was: #{element}"
end