Skip to content

Instantly share code, notes, and snippets.

@tgaff
Last active November 5, 2015 17:55
Show Gist options
  • Save tgaff/b9f84c1bda4a5f7ea055 to your computer and use it in GitHub Desktop.
Save tgaff/b9f84c1bda4a5f7ea055 to your computer and use it in GitHub Desktop.
ruby looping
arr = [ 2, 4, 6, 1, 7, 9]
arr.each do |element|
puts "I'm #{element}."
end
arr.each_with_index do |element, index|
puts "#{element} has index #{index}"
end
3.times do
puts 'ok'
end
arr.length.times do |index|
puts "#{arr[index]} has index #{index}"
end
something = true
while something do # while something is true
puts "Do stuff"
something = false if gets.chomp == 'stop'
end
something_else = false
until something_else do # do this block until something_else is true
puts "Do stuff"
something_else = true if gets.chomp == 'stop'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment