Skip to content

Instantly share code, notes, and snippets.

@betamatt
Created October 14, 2010 17:56
Show Gist options
  • Save betamatt/626666 to your computer and use it in GitHub Desktop.
Save betamatt/626666 to your computer and use it in GitHub Desktop.
# Iterate a list
a = [5,6,7,8]
a.each_with_index{ |x, i| puts "#{i} => #{x}" }
# Conditionally end a loop
stop = false
while(!stop) do
value = rand(5)
puts "Picked #{value}"
stop = true if 0 == value
end
# Swap an item in an array with the item before it
a = [1,2,3]
holder = a[2] # think of how you juggle three balls with two hands.
a[2] = a[1]
a[1] = holder
# a is now [1,3,2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment