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

repl do
  puts "* RUBY PRIMER: Loops"
end

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

repl do
  puts "** INFO: ~Numeric#times~ (for loop)"
  3.times do |i|
    puts i
  end
end

# ====================================================================================
#  foreach
# ====================================================================================

repl do
  puts "** INFO: ~Array#each~ (for each loop)"
  array = ["a", "b", "c", "d"]
  array.each do |char|
    puts char
  end

  puts "** INFO: ~Array#each_with_index~ (for each loop)"
  array = ["a", "b", "c", "d"]
  array.each do |char, i|
    puts "index #{i}: #{char}"
  end
end

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

repl do
  puts "** INFO: range block exclusive (three dots)"
  (0...3).each do |i|
    puts i
  end

  puts "** INFO: range block inclusive (two dots)"
  (0..3).each do |i|
    puts i
  end
end