Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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]
Add Comment
Please, Sign In to add comment