0
0
Rubyprogramming~10 mins

Array creation methods in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array creation methods
Start
Choose method
Literal: arr = [
Array.new(size)
Array.new(size, default_value)
Array.new(size) { block }
Array created
Use array
You pick a way to create an array, Ruby builds it, then you use it.
Execution Sample
Ruby
arr1 = []
arr2 = Array.new(3)
arr3 = Array.new(3, 0)
arr4 = Array.new(3) { |i| i * 2 }
Creates arrays using different Ruby methods: empty, fixed size nils, fixed size with default, and with a block.
Execution Table
StepActionCode LineArray StateNotes
1Create empty arrayarr1 = [][]Empty array with zero elements
2Create array with 3 nilsarr2 = Array.new(3)[nil, nil, nil]Size 3, default nil
3Create array with 3 zerosarr3 = Array.new(3, 0)[0, 0, 0]Size 3, default 0 (same object)
4Create array with blockarr4 = Array.new(3) { |i| i * 2 }[0, 2, 4]Size 3, values from block using index
5End--All arrays created successfully
💡 All array creation lines executed, arrays ready for use.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
arr1undefined[][][][][]
arr2undefinedundefined[nil, nil, nil][nil, nil, nil][nil, nil, nil][nil, nil, nil]
arr3undefinedundefinedundefined[0, 0, 0][0, 0, 0][0, 0, 0]
arr4undefinedundefinedundefinedundefined[0, 2, 4][0, 2, 4]
Key Moments - 3 Insights
Why does arr3 have three zeros but they all point to the same object?
Because Array.new(3, 0) uses the same object 0 for all elements, so changing one changes all. See step 3 in execution_table.
How does the block in Array.new(3) { |i| i * 2 } work?
The block runs for each index from 0 to 2, setting each element to i*2. This creates distinct values. See step 4 in execution_table.
What is the difference between arr2 and arr3?
arr2 has three nils (default nil), arr3 has three zeros (default 0 object). arr2 elements are nil, arr3 elements are the same zero object. See steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the content of arr2?
A[nil, nil, nil]
B[0, 0, 0]
C[]
D[1, 2, 3]
💡 Hint
Check the 'Array State' column at step 2 in execution_table.
At which step does the array get values from a block?
AStep 1
BStep 4
CStep 3
DStep 2
💡 Hint
Look for 'block' keyword in the 'Action' column in execution_table.
If we change arr3 to Array.new(3) { 0 }, how would the array elements differ?
AThe array would be empty
BAll elements would be the same object 0
CEach element would be a separate 0 object
DThe array would contain nils
💡 Hint
Compare Array.new(size, obj) vs Array.new(size) { block } behavior from variable_tracker.
Concept Snapshot
Array creation methods in Ruby:
- Literal: arr = [] creates empty array
- Array.new(size) creates array with nils
- Array.new(size, obj) fills with same obj
- Array.new(size) { block } fills with block values
Use block to avoid shared objects.
Full Transcript
This visual trace shows how Ruby creates arrays using different methods. First, an empty array is made with []. Then Array.new(3) creates an array with three nil elements. Next, Array.new(3, 0) creates an array with three zeros, but all zeros are the same object. Finally, Array.new(3) with a block sets each element to a unique value based on the index. The variable tracker shows how each array changes after each step. Key moments explain why shared objects happen and how blocks create distinct elements. The quiz tests understanding of array contents and creation differences.