Arrays help you store many items together in one place. Creating arrays is the first step to use them.
Array creation methods in Ruby
Introduction
Syntax
Ruby
array = [item1, item2, item3] empty_array = [] array = Array.new(size) array = Array.new(size, default_value)
You can create arrays by listing items inside square brackets [].
Array.new creates a new array with a given size and optional default value.
Examples
Ruby
fruits = ["apple", "banana", "cherry"]
Ruby
empty_list = []
Ruby
numbers = Array.new(3)
Ruby
zeros = Array.new(4, 0)
Sample Program
This program shows different ways to create arrays and prints them.
Ruby
fruits = ["apple", "banana", "cherry"] puts "Original fruits array:" puts fruits.inspect empty_array = [] puts "Empty array:" puts empty_array.inspect numbers = Array.new(3) puts "Array with 3 nils:" puts numbers.inspect zeros = Array.new(4, 0) puts "Array with 4 zeros:" puts zeros.inspect
Important Notes
Creating arrays with Array.new(size, value) sets all elements to the same object, so be careful with mutable objects.
Using [] is the simplest way to create arrays with known items.
Empty arrays are useful when you want to add items later.
Summary
Arrays store multiple items in one variable.
You can create arrays by listing items or using Array.new.
Empty arrays and arrays with default values are common starting points.
Practice
1. Which of the following is a correct way to create an empty array in Ruby?
easy
Solution
Step 1: Understand Ruby array syntax
In Ruby, arrays are created using square brackets[]for literals.Step 2: Identify correct empty array syntax
[]creates an empty array, while{}creates an empty hash, and the other options are not Ruby syntax.Final Answer:
[] -> Option BQuick Check:
Empty array = [] [OK]
Hint: Empty arrays use square brackets [] in Ruby [OK]
Common Mistakes:
- Using curly braces {} which creates a hash
- Using parentheses like array() which is invalid
- Using JavaScript-like syntax new Array()
2. Which of the following is the correct syntax to create an array with 3 elements, all initialized to zero?
easy
Solution
Step 1: Recall Array.new syntax
In Ruby,Array.new(size, default_value)creates an array with given size and fills it with the default value.Step 2: Check each option
Array.new(3, 0)correctly creates [0, 0, 0].Array.new(0, 3)creates an empty array since size is first argument. Options C and D are invalid syntax.Final Answer:
Array.new(3, 0) -> Option AQuick Check:
Array.new(3, 0) = [0, 0, 0] [OK]
Hint: Use Array.new(size, value) to create arrays with default values [OK]
Common Mistakes:
- Confusing Array.new arguments order
- Using invalid syntax like Array(0, 3)
- Trying JavaScript style new Array()
3. What is the output of the following Ruby code?
arr = Array.new(4) { |i| i * 2 }
puts arr.inspectmedium
Solution
Step 1: Understand Array.new with block
When Array.new is called with a block, it runs the block for each index, passing the index asi.Step 2: Calculate each element
For indices 0 to 3, the block returnsi * 2: 0*2=0, 1*2=2, 2*2=4, 3*2=6.Final Answer:
[0, 2, 4, 6] -> Option CQuick Check:
Array with block fills elements by index * 2 [OK]
Hint: Array.new with block uses index to set each element [OK]
Common Mistakes:
- Assuming default value 0 for all elements
- Confusing block argument meaning
- Expecting nil values without block
4. The following code is intended to create an array of 3 separate empty arrays. What is the problem?
arr = Array.new(3, []) arr[0] << 1 puts arr.inspect
medium
Solution
Step 1: Understand Array.new with default object
UsingArray.new(3, [])creates 3 references to the same empty array, not 3 separate arrays.Step 2: Analyze mutation effect
Whenarr[0] << 1runs, it adds 1 to the shared array, so all elements show the change.Final Answer:
[[1], [1], [1]] -> Option AQuick Check:
Same object repeated causes all elements to change [OK]
Hint: Use block with Array.new for separate objects [OK]
Common Mistakes:
- Expecting separate arrays but all share one object
- Not using block form for unique elements
- Assuming << affects only one element
5. How can you create an array of 5 hashes, each with keys :a and :b initialized to 0, ensuring each hash is a separate object?
hard
Solution
Step 1: Understand object references in Array.new
UsingArray.new(5, {a: 0, b: 0})or[{a: 0, b: 0}] * 5repeats the same hash object 5 times.Step 2: Use block form for unique hashes
UsingArray.new(5) { {a: 0, b: 0} }runs the block 5 times, creating 5 separate hashes.Final Answer:
Array.new(5) { {a: 0, b: 0} } -> Option DQuick Check:
Block form creates unique objects [OK]
Hint: Use block with Array.new for unique hashes [OK]
Common Mistakes:
- Using Array.new with default hash causes shared references
- Using multiplication operator with arrays of hashes
- Trying invalid Array() constructor syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
