Created
October 14, 2010 17:56
-
-
Save betamatt/626666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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