Bird
0
0

Find the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Arrays
Find the error in this Ruby code:
arr = [1, 2, 3]
puts arr.include(2)
Ainclude? method is misspelled
Binclude? method should be called with a question mark
Cinclude? requires a block
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check method spelling and syntax

    The method to check element presence is include? with a question mark at the end.
  2. Step 2: Identify the mistake in code

    The code uses include without the question mark, which causes a NoMethodError.
  3. Final Answer:

    include? method should be called with a question mark -> Option B
  4. Quick Check:

    include? requires question mark [OK]
Quick Trick: Remember include? ends with ? to work correctly [OK]
Common Mistakes:
  • Omitting question mark in include?
  • Thinking include? needs a block
  • Assuming include without ? works

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes