📖 Scenario: You are helping a small bookstore organize a list of book titles. You want to practice how to get specific books from the list by their position.
🎯 Goal: Learn how to access elements in a list by using index numbers, and how to get the first and last items easily.
📋 What You'll Learn
Create a list of book titles with exact names
Create a variable to hold the index number to access
Use the index to get the correct book from the list
Print the first book, the book at the index, and the last book
💡 Why This Matters
🌍 Real World
Accessing elements by position is common when working with lists of items like books, songs, or products.
💼 Career
Knowing how to get specific data from lists is a basic skill for programming jobs that handle collections of information.
Progress0 / 4 steps
1
Create the list of books
Create an array called books with these exact titles in this order: 'The Hobbit', '1984', 'Pride and Prejudice', 'To Kill a Mockingbird', 'The Great Gatsby'
Ruby
Hint
Use square brackets [] to create the array and separate titles with commas.
2
Set the index to access
Create a variable called index and set it to 2 to choose the third book in the list
Ruby
Hint
Remember, arrays start counting at 0, so 2 means the third item.
3
Access the book at the index
Create a variable called selected_book and set it to the book at position index in the books array
Ruby
Hint
Use square brackets with the variable index to get the book.
4
Print first, selected, and last books
Print the first book using books.first, the selected book using selected_book, and the last book using books.last each on its own line
Ruby
Hint
Use puts to print each book on its own line.
Practice
(1/5)
1. Which method or syntax in Ruby returns the first element of an array arr?
easy
A. arr.last
B. arr[1]
C. arr[-1]
D. arr.first
Solution
Step 1: Understand the purpose of .first
The .first method returns the first element of an array in Ruby.
Step 2: Compare with other options
arr[1] returns the second element (indexing starts at 0), arr.last and arr[-1] return the last element.
Final Answer:
arr.first -> Option D
Quick Check:
First element = arr.first [OK]
Hint: Use .first to get the first element easily [OK]
Common Mistakes:
Using arr[1] instead of arr[0] for first element
Confusing .last with .first
Using negative index for first element
2. Which of the following is the correct syntax to get the last element of an array arr in Ruby?
easy
A. arr.first
B. arr[0]
C. arr[-1]
D. arr[1]
Solution
Step 1: Understand negative indexing in Ruby arrays
Negative indexes count from the end, so arr[-1] returns the last element.
Step 2: Check other options
arr[0] is the first element, arr.first also returns first, and arr[1] is second element.
Final Answer:
arr[-1] -> Option C
Quick Check:
Last element = arr[-1] [OK]
Hint: Use negative index -1 to get last element [OK]
arr.first is 10 (first element), arr[-2] is second last element, 40.
Final Answer:
30
10
40 -> Option B
Quick Check:
Index 2 = 30, first = 10, second last = 40 [OK]
Hint: Remember index starts at 0; negative counts from end [OK]
Common Mistakes:
Confusing index 2 with index 3
Mixing up first and last elements
Misunderstanding negative indexes
4. The following Ruby code throws an error. What is the problem?
arr = [5, 10, 15]
puts arr.first()
puts arr[-4]
medium
A. Negative index -4 is out of range and returns nil, no error
B. Missing argument in first() causes error
C. Array is empty, so first() fails
D. Syntax error in calling first()
Solution
Step 1: Check arr.first() usage
first() can be called with or without parentheses; no error here.
Step 2: Analyze arr[-4]
Array has 3 elements, so index -4 is out of range and returns nil, which prints as empty line, no error.
Final Answer:
Negative index -4 is out of range and returns nil, no error -> Option A
Quick Check:
Out of range negative index returns nil [OK]
Hint: Out of range negative index returns nil, no error [OK]
Common Mistakes:
Assuming out of range index causes error
Thinking first() needs arguments
Confusing nil with error
5. Given the array arr = ["a", "b", "c", "d", "e"], which Ruby expression returns a new array with only the first and last elements?
hard
A. [arr.first, arr.last]
B. arr[0, 1] + arr[-1]
C. arr[0] + arr[-1]
D. arr.first + arr.last
Solution
Step 1: Understand the goal
We want a new array containing only the first and last elements.
Step 2: Evaluate each option
[arr.first, arr.last] creates an array with arr.first and arr.last. arr[0, 1] + arr[-1] tries to add an array and a string (wrong). arr[0] + arr[-1] adds two strings, resulting in concatenation, not array. arr.first + arr.last adds two strings, also concatenation.
Final Answer:
[arr.first, arr.last] -> Option A
Quick Check:
Array with first and last elements = [arr.first, arr.last] [OK]
Hint: Use array brackets to combine first and last elements [OK]
Common Mistakes:
Adding strings instead of creating an array
Using + with array and string causing error
Forgetting to wrap elements in array brackets
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.