0
0
Rubyprogramming~15 mins

Why arrays are fundamental in Ruby - See It in Action

Choose your learning style9 modes available
Why arrays are fundamental in Ruby
📖 Scenario: Imagine you are organizing a small library of books. You want to keep track of the titles in the order they arrive. Arrays in Ruby help you do this easily.
🎯 Goal: You will create an array of book titles, add a new book, find how many books you have, and then print the list of books.
📋 What You'll Learn
Create an array called books with three exact book titles
Create a variable called new_book with a specific book title
Add the new_book to the books array
Create a variable called total_books that stores the number of books in the books array
Print the books array
💡 Why This Matters
🌍 Real World
Arrays are used everywhere to keep lists of things like books, songs, or tasks in order.
💼 Career
Understanding arrays is essential for any programming job because they help manage collections of data efficiently.
Progress0 / 4 steps
1
Create an array of book titles
Create an array called books with these exact titles: 'The Hobbit', '1984', and 'Pride and Prejudice'.
Ruby
Need a hint?

Use square brackets [] to create an array and separate items with commas.

2
Add a new book title
Create a variable called new_book and set it to the string 'To Kill a Mockingbird'.
Ruby
Need a hint?

Use = to assign the string to the variable new_book.

3
Add the new book to the array
Add the new_book to the end of the books array using the push method.
Ruby
Need a hint?

Use array.push(item) to add an item to the end of an array.

4
Count and print the books
Create a variable called total_books that stores the number of books in the books array using the length method. Then print the books array.
Ruby
Need a hint?

Use array.length to get the number of items. Use puts to print the array.