Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ARRAY
- # A collection of INDEXED elements
- # Initialize empty array
- arr = []
- # Append an element
- arr.push("Paul")
- arr << "Ringo"
- arr << "George"
- arr << "John"
- # Address an element
- array[0] # => "Paul"
- array.first # => "Paul"
- array[array.size - 1] # => return the last element
- array.last # => same
- array[-1] # => same
- # [55] pry(main)> array[-3]
- # Remove an element
- # .pop — remove it from the end AND RETURS IT
- # remove not from the end:
- # .delete_at(index)
- # .delete(value) # Note it will remove ALL OCCURENCES OF VALUE
- # Change an element: 2-step operation: get element by index and assign new value
- arrray[1] = "Andy"
- # Iterate over an array with .each
- array.each do |person|
- puts person
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement