Bird
0
0

Which of the following is the correct syntax to define a method hello inside a class created with Class.new?

easy📝 Syntax Q3 of 15
Ruby - Metaprogramming Fundamentals
Which of the following is the correct syntax to define a method hello inside a class created with Class.new?
AMyClass = Class.new do def hello 'Hi'; end end
BMyClass = Class.new do def hello() 'Hi' end end
CMyClass = Class.new { def hello 'Hi' end }
DMyClass = Class.new { def hello; 'Hi' }
Step-by-Step Solution
Solution:
  1. Step 1: Understand block syntax for Class.new

    Using do...end block with method definition inside is valid.
  2. Step 2: Check method syntax

    Method definitions require def method_name() ... end or def method_name; ... end. MyClass = Class.new do def hello() 'Hi' end end uses parentheses and block correctly.
  3. Final Answer:

    MyClass = Class.new do def hello() 'Hi' end end -> Option B
  4. Quick Check:

    Method inside Class.new block uses do...end with def [OK]
Quick Trick: Use do...end block with def inside Class.new for methods [OK]
Common Mistakes:
  • Mixing braces and do...end incorrectly
  • Omitting method end keyword
  • Using invalid block syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes