Ruby - ArraysFind the mistake in this Ruby code:arr = [10, 20, 30] puts arr.first(2)Aarr.first(2) returns an array, puts expects stringBarr.first(2) is invalid syntaxCarr.first(2) returns nilDNo mistake, code runs fineCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand arr.first(2) return typearr.first(2) returns an array of first two elements: [10, 20].Step 2: Check puts behavior with arraysputs prints each element of the array on a new line without error.Final Answer:No mistake, code runs fine -> Option DQuick Check:puts can print arrays element-wise [OK]Quick Trick: puts prints arrays element-wise without error [OK]Common Mistakes:MISTAKESThinking puts needs string onlyBelieving arr.first(2) is invalidExpecting nil return
Master "Arrays" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Array methods (length, include?, flatten) - Quiz 10hard Arrays - Array methods (length, include?, flatten) - Quiz 5medium Control Flow - Inline if and unless (modifier form) - Quiz 6medium Hashes - Default values for missing keys - Quiz 9hard Loops and Iteration - For loop (rarely used in Ruby) - Quiz 2easy Methods - Method naming conventions (? and ! suffixes) - Quiz 14medium Methods - Why methods always return a value in Ruby - Quiz 14medium Operators and Expressions - Arithmetic operators - Quiz 6medium Ruby Basics and Runtime - What is Ruby - Quiz 11easy Variables and Data Types - Integer and Float number types - Quiz 5medium