Bird
0
0

Which of the following is the correct syntax to define a method named greet using define_method that returns "Hello"?

easy📝 Syntax Q12 of 15
Ruby - Advanced Metaprogramming
Which of the following is the correct syntax to define a method named greet using define_method that returns "Hello"?
Adef define_method(:greet) "Hello" end
Bdefine_method greet { return "Hello" }
Cdefine_method(:greet) { "Hello" }
Ddefine_method(:greet, "Hello")
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct define_method syntax

    The correct syntax uses a symbol for the method name and a block: define_method(:name) { ... }.
  2. Step 2: Check each option

    def define_method(:greet) "Hello" end uses def incorrectly. define_method greet { return "Hello" } lacks symbol (greet undefined). define_method(:greet, "Hello") passes string instead of block. B matches correct syntax.
  3. Final Answer:

    define_method(:greet) { "Hello" } -> Option C
  4. Quick Check:

    Correct syntax uses symbol and block [OK]
Quick Trick: Use symbol and block for define_method syntax [OK]
Common Mistakes:
  • Using method name without symbol
  • Incorrect block syntax
  • Using def inside define_method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes