Arrays help you keep many items together in one place. They make it easy to store, find, and use groups of things in Ruby.
Why arrays are fundamental in Ruby
class Array # You create an array with square brackets [] # Example: my_array = [1, 2, 3] # You can add items with push or << # Example: my_array.push(4) # Or: my_array << 5 # You get items by index starting at 0 # Example: my_array[0] # returns first item end
Arrays in Ruby can hold any type of item: numbers, words, or even other arrays.
Indexes start at 0, so the first item is at position 0.
empty_array = []
puts empty_array.inspectsingle_item_array = [42] puts single_item_array[0]
fruits = ["apple", "banana", "cherry"] puts fruits[2]
numbers = [] numbers << 10 numbers.push(20) puts numbers.inspect
This program shows how to create an array, add items, access an item by index, and loop through all items.
puts "Create an empty array for colors:" colors = [] puts colors.inspect puts "Add colors to the array:" colors << "red" colors.push("green") colors << "blue" puts colors.inspect puts "Access the first color:" puts colors[0] puts "Loop through all colors:" colors.each do |color| puts "Color: #{color}" end
Arrays are fast to access by index (O(1) time).
Adding items at the end is usually fast (amortized O(1)).
Common mistake: forgetting that indexes start at 0, so the first item is at index 0, not 1.
Use arrays when you need ordered collections and quick access by position.
Arrays store many items together in order.
You can add, access, and loop through items easily.
They are a basic and powerful tool in Ruby programming.
Practice
Solution
Step 1: Understand what arrays do
Arrays hold many items together in a specific order, making it easy to manage groups of data.Step 2: Compare options with array features
Only Because they store multiple items in an ordered list correctly describes arrays as ordered collections of multiple items.Final Answer:
Because they store multiple items in an ordered list -> Option BQuick Check:
Arrays = ordered collections [OK]
- Thinking arrays only hold numbers
- Believing arrays are immutable
- Confusing arrays with file operations
Solution
Step 1: Recall Ruby array syntax
Arrays in Ruby are created using square brackets [] with items separated by commas.Step 2: Check each option's syntax
Only array = [1, 2, 3] uses square brackets correctly; others use parentheses, braces, or angle brackets which are invalid for arrays.Final Answer:
array = [1, 2, 3] -> Option AQuick Check:
Arrays use [] brackets [OK]
- Using parentheses instead of brackets
- Confusing arrays with hashes (curly braces)
- Using angle brackets which are invalid
arr = [10, 20, 30] puts arr[1]
Solution
Step 1: Understand array indexing in Ruby
Ruby arrays start at index 0, so arr[0] is 10, arr[1] is 20, arr[2] is 30.Step 2: Identify the value at index 1
The code prints arr[1], which is 20.Final Answer:
20 -> Option AQuick Check:
arr[1] = 20 [OK]
- Thinking array index starts at 1
- Confusing values at different indexes
- Expecting nil for valid index
numbers = [1, 2, 3] numbers.add(4)
Solution
Step 1: Check the method used to add items to arrays
Ruby arrays use 'push' or '<<' to add items, not 'add'.Step 2: Identify the error cause
Calling 'add' on an array causes a NoMethodError because it is not a valid method.Final Answer:
The method 'add' does not exist for arrays -> Option CQuick Check:
Use 'push' or '<<' to add items [OK]
- Using 'add' instead of 'push' or '<<'
- Thinking arrays cannot be changed
- Adding numbers as strings unnecessarily
arr = [1, 2, 3, 4, 5], which Ruby code correctly creates a new array with only even numbers?Solution
Step 1: Understand the goal
We want a new array containing only even numbers from the original array.Step 2: Analyze each option
new_arr = arr.select { |num| num % 2 == 0 } uses 'select' to filter even numbers correctly. new_arr = arr.map { |num| num * 2 } doubles all numbers, not filtering. new_arr = arr.delete_if { |num| num.even? } deletes even numbers, opposite of goal. new_arr = arr.push(2, 4) adds 2 and 4 to original array, not filtering.Final Answer:
new_arr = arr.select { |num| num % 2 == 0 } -> Option DQuick Check:
Use 'select' to filter arrays [OK]
- Using 'map' instead of 'select' for filtering
- Deleting instead of selecting items
- Adding items instead of filtering
Ready to prove it?
Pass the test and this pattern moves unknown
