Loop

Regular:#

# both 0 and 2 are inclusive
for count in 0..2
puts "Value of count is: #{count}"
end
# Output:
Value of count is: 0
Value of count is: 1
Value of count is: 2

Similar to Java for-each loop#

  • Java
int[] nums = { 'one', 'two', 'three', 'four' }
for(int num: nums){
System.out.println(num);
}
  • Ruby
nums = ['one', 'two', 'three', 'four']
for num in nums
puts num
end

Times:#

10.times { |i| puts "hello #{i}" }

Range#

(1..10).each { |i| puts i }

Stop a Loop?#

break # java: break;

Skip a loop?#

next # java: continue

Redo?#

  • Restarts current iteration
  • TODO

Retry?#

  • TODO