Bird
0
0

You want to quickly test a method in IRB that returns the square of a number. Which of these is the correct way to define and test it?

hard📝 Application Q15 of 15
Ruby - Basics and Runtime
You want to quickly test a method in IRB that returns the square of a number. Which of these is the correct way to define and test it?
Adef square(x); x * x; end; square(4)
Bdef square x x * x end square 4
Cdef square(x) { return x * x }; square(4)
Ddef square(x): return x * x end; square(4)
Step-by-Step Solution
Solution:
  1. Step 1: Check correct Ruby method syntax

    Ruby methods use def name(params); body; end syntax. def square(x); x * x; end; square(4) follows this correctly.
  2. Step 2: Verify method call syntax

    Calling square(4) is correct. The other options have syntax errors or wrong Ruby style.
  3. Final Answer:

    def square(x); x * x; end; square(4) -> Option A
  4. Quick Check:

    Method def/end with parentheses is correct [OK]
Quick Trick: Use def ... end with parentheses for methods [OK]
Common Mistakes:
MISTAKES
  • Using braces {} for method body
  • Omitting parentheses incorrectly
  • Using Python-like syntax with colons

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes