summaryrefslogtreecommitdiffhomepage
path: root/samples/00_intermediate_ruby_primer/app/05_looping.txt
blob: fafdcfcfad629e6ad0d68eede3a57c758983dff4 (plain)
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
# ====================================================================================
#  Looping
# ====================================================================================
#
# Looping looks a whole lot different than other languages.
# But it's pretty awesome when you get used to it.

puts ''
puts ''
puts '================================'
puts ''

# ====================================================================================
#  times
# ====================================================================================


puts "times block:"
3.times do |i|
  puts i
end
puts ''


# ====================================================================================
#  ranges
# ====================================================================================

puts "range block exclusive:"
(0...3).each do |i|
  puts i
end
puts ''

puts "range block inclusive:"
(0..3).each do |i|
  puts i
end
puts ''

# ====================================================================================
#  Enumerables
# ====================================================================================

puts 'array each'
colors = ["red", "blue", "yellow"]
colors.each do |color|
  puts color
end
puts ''

puts 'array each_with_index'
colors = ["red", "blue", "yellow"]
colors.each_with_index do |color, i|
  puts "#{color} at index #{i}"
end

puts ''
puts '================================'
puts ''
puts ''