Ruby: for, while, Loop

By Xah Lee. Date:

“for” Loop

#-*- coding: utf-8 -*-
# ruby

aa = 1..9 # creates a array 1 to 9
for xx in aa do
  p xx
end # prints 1 to 9

“while” Loop

#-*- coding: utf-8 -*-
# ruby

x = 1;
while x <= 9 do
  p x
  x += 1
end

Exiting Loop

#-*- coding: utf-8 -*-
# ruby

for xx in 1..9 do
  p xx
  if xx == 4 then break end
end # prints 1 to 4