Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. # ARRAY
  2.  
  3. # A collection of INDEXED elements
  4.  
  5. # Initialize empty array
  6. arr = []
  7.  
  8. # Append an element
  9. arr.push("Paul")
  10. arr << "Ringo"
  11. arr << "George"
  12. arr << "John"
  13.  
  14. # Address an element
  15. array[0] # => "Paul"
  16. array.first # => "Paul"
  17.  
  18. array[array.size - 1] # => return the last element
  19. array.last # => same
  20. array[-1] # => same
  21. # [55] pry(main)> array[-3]
  22.  
  23. # Remove an element
  24.  
  25. # .pop — remove it from the end AND RETURS IT
  26.  
  27. # remove not from the end:
  28. # .delete_at(index)
  29. # .delete(value) # Note it will remove ALL OCCURENCES OF VALUE
  30.  
  31. # Change an element: 2-step operation: get element by index and assign new value
  32. arrray[1] = "Andy"
  33.  
  34. # Iterate over an array with .each
  35. array.each do |person|
  36. puts person
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement