-
Notifications
You must be signed in to change notification settings - Fork 2
/
ex32.rb
39 lines (33 loc) · 1003 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
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 a list
# in a more traditional style in other languages
the_count.each do |number|
puts "This is count #{number}"
end
# same as above, but in a more Ruby style
# this and the next are the preferred
# way Ruby for-loops are written
fruits.each do |fruit|
puts "A fruit of type: #{fruit}"
end
# also we can go through mixed lists too
# note this is yet another style, exactly like above
# but a different syntax (way to write it)
change.each {|i|
fruits.each{|j|
puts "#{j}"
}
puts "I got #{i}"
}
# we can also build lists, first start with an empty one
elements = []
# then use the range operator to do 0 to 5 counts
(0..5).each do |i|
puts "adding #{i} to the list."
# pushes the i variable on the *end* of the list
elements.push(i)
end
# now we can print them out too
elements.each {|i| puts "Element was #{i}"}