Guest User

Untitled

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. # Iterate a list
  2. a = [5,6,7,8]
  3. a.each_with_index{ |x, i| puts "#{i} => #{x}" }
  4.  
  5. # Conditionally end a loop
  6. stop = false
  7. while(!stop) do
  8. value = rand(5)
  9. puts "Picked #{value}"
  10. stop = true if 0 == value
  11. end
  12.  
  13. # Swap an item in an array with the item before it
  14. a = [1,2,3]
  15. holder = a[2] # think of how you juggle three balls with two hands.
  16. a[2] = a[1]
  17. a[1] = holder
  18. # a is now [1,3,2]
Add Comment
Please, Sign In to add comment