Bird
0
0

Which of the following is the correct syntax to define a hook method named before_save in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Advanced Metaprogramming
Which of the following is the correct syntax to define a hook method named before_save in Ruby?
Adef hook :before_save; puts 'Saving'; end
Bhook before_save { puts 'Saving' }
Cbefore_save do puts 'Saving' end
Ddef before_save; puts 'Saving'; end
Step-by-Step Solution
Solution:
  1. Step 1: Recognize Ruby method definition syntax

    Ruby methods are defined with def method_name; ... end.
  2. Step 2: Match the hook method name

    The hook method before_save is correctly defined as def before_save; ... end.
  3. Final Answer:

    def before_save; puts 'Saving'; end -> Option D
  4. Quick Check:

    Ruby method syntax = A [OK]
Quick Trick: Ruby methods use def name; ... end syntax [OK]
Common Mistakes:
  • Using non-Ruby syntax like 'hook' keyword
  • Missing 'def' keyword
  • Incorrect block syntax without do/end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes