Which of these is the correct syntax to add an element to an array named 'arr'?
easy📝 Syntax Q3 of 15
Ruby - Arrays
Which of these is the correct syntax to add an element to an array named 'arr'?
Aarr << 5
Barr.add(5)
Carr.push 5, 6
Darr.insert(5)
Step-by-Step Solution
Solution:
Step 1: Recall Ruby syntax for adding elements to arrays
The '<<' operator appends a single element to the array.
Step 2: Check each option's validity
arr << 5 uses '<<' correctly to append a single element; arr.push 5, 6 is valid syntax but adds two elements; arr.add(5) is not a method on arrays; arr.insert(5) requires an index and value.
Final Answer:
arr << 5 -> Option A
Quick Check:
Add element syntax = arr << element [OK]
Quick Trick:Use '<<' to append elements to arrays [OK]
Common Mistakes:
Using add instead of <<
Incorrect push syntax
Misusing insert without index
Master "Arrays" in Ruby
9 interactive learning modes - each teaches the same concept differently