0
0
Rubyprogramming~15 mins

Array creation methods in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Array creation methods
📖 Scenario: You are organizing a small library and want to keep track of book titles using arrays in Ruby.
🎯 Goal: Build a Ruby program that creates arrays of book titles using different array creation methods.
📋 What You'll Learn
Create an array called books with three specific book titles.
Create a variable called empty_books that holds an empty array.
Create an array called default_books with 3 elements, each set to the string 'Unknown'.
Print the contents of all three arrays.
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of items like book titles, names, or numbers in many programs.
💼 Career
Knowing how to create and manage arrays is essential for software development, data processing, and scripting tasks.
Progress0 / 4 steps
1
Create an array with book titles
Create an array called books with these exact book titles: 'The Hobbit', '1984', and 'Brave New World'.
Ruby
Need a hint?

Use square brackets [] to create an array with the given strings separated by commas.

2
Create an empty array
Create a variable called empty_books and set it to an empty array using [].
Ruby
Need a hint?

Use empty square brackets [] to create an empty array.

3
Create an array with default values
Create an array called default_books with 3 elements, each set to the string 'Unknown', using the Array.new method.
Ruby
Need a hint?

Use Array.new(3, 'Unknown') to create an array with 3 copies of 'Unknown'.

4
Print all arrays
Print the arrays books, empty_books, and default_books each on its own line using puts.
Ruby
Need a hint?

Use puts array.inspect to print the array contents clearly.