Arrays and Common Methods

Arrays in Ruby are ordered collections of elements.

Creating Arrays:

fruits = ["apple", "banana", "cherry"]

Accessing Elements:

puts fruits[0]    # apple
puts fruits[-1]   # cherry

Common Methods:

  • push or <<: Add elements
  • pop: Remove last element
  • each: Iterate over elements
  • map, select, reject: Functional operations
← PrevNext →