Ruby Loops

loop do
  print 'Do you want to continue ? (y/n)'
  answer = gets.chomp.downcase
  if answer == 'n'
    break
  end
end

only if takes one line of code

loop {
  print 'Do you want to continue ? (y/n)'
  answer = gets.chomp.downcase
}

Generate random number with a max digit

random_number = Random.new.rand(5)
loop do
  print 'Guess the number between 0 and 5 (e to exit)'
  answer = gets.chomp
  if answer == 'e'
    puts 'The number was #{random_number}.'
    break
  else
    if answer.to_i == random_number
      puts 'You guessed correctly!'
      break
    else
      puts 'Try again'
    end
  end
end

Store input as integer

loop do
  print 'Enter a number greater than 10 to exit: '
  answer = gets.chomp.to_i
  if answer > 10
    break
  end
end

def get_name
  name = ''
  loop do
    print 'Enter your name: '
    if name.length >= 2 && !name.index(/\d/)
      break
    else
      puts 'Name must be longer than 2 characters and not contain numbers.'
    end
  end
  return name
end

name = get_name
puts "Hi #{name}."
While Loop

Break is not needed

answer = ''
while answer != 'n'
  print 'Do you want me to repeat this pointless loop again? (n/y)'
  answer = gets.chomp.downcase
end

def print_hello(number_of_times)
  i = 0
  while i < number_of_times
    puts 'Hello'
    i += 1
  end
end

answer = 0
while answer < 5
  print 'How many times do you want to print hello? (Enter a number greater than 5 to exit)'
  answer = gets.chomp.to_i
  print_hello(answer)
end

define answer as empty string, or else it would be nil

answer = ''
until answer == 'no' do
  print 'Do you want this loop to continue? (y/n)'
  answer = gets.chomp
end

until/while loops use do optionally

Iteration with each

All three examples iterate over the array

array = [0, 1, 2, 3, 4, 5]
i = 0
while i < array.length
  item = array[i]
  puts 'The current item is #{item}.'
  i += 1
end

array = [0, 1, 2, 3, 4, 5]
array.each do |item|
  puts 'The current item is #{item}.'
end

array = [0, 1, 2, 3, 4, 5]
array.each { |item| puts 'The current item is #{item}.'}
array = [0, 1, 2, 3, 4, 5]
array.each do |n|
  puts "#{n}"
end

Array is unmodified by each iteration

array = [0, 1, 2, 3, 4, 5]
array.each do |item|
  item = item + 2
  puts 'The current item + 2 is #{item}.'
end
puts array.inspect

business = {'name'=> 'Treehouse', 'location' => 'Portland', 'OR'}
business.each do |key, value| # each method is also each_pair
  puts 'The hash key is #{key} and the value is #{value}'
end

business.each_key do |key|
  puts 'Key: #{key}'
end
Times Iteration

Will take a number and execute the statements in the block that number of times The times iterator optionally takes an argument and, just like array indices, starts at 0

5.times do
  puts 'Hello!'
end

accepts one args

5.times do |item|
  puts 'Hello! #{item}'
end
For Loop
for item in 1..10 do # range separated by two dots
  puts 'The current item is #{item}.'
end

variables exist outside of the loop

for item in ['Programming', 'is', 'fun']
  puts 'The current item is #{item}.'
end

animals = ["dog", "cat", "horse", "goat"]
for item in animals
  puts "#{item}"
end