Bird
0
0

Which of the following is the correct way to declare a private method in a Ruby class?

easy📝 Syntax Q3 of 15
Ruby - Inheritance
Which of the following is the correct way to declare a private method in a Ruby class?
Aprivate\ndef method_name\nend
Bprivate def method_name; end
Cdef private method_name; end
Ddef method_name; private; end
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby private method syntax

    In Ruby, you declare private methods by writing private on a line, then defining methods below it.
  2. Step 2: Check each option

    private\ndef method_name\nend correctly uses private followed by method definition. Options B, C, and D are invalid syntax.
  3. Final Answer:

    private\ndef method_name\nend -> Option A
  4. Quick Check:

    Private method declaration = private\ndef method_name\nend [OK]
Quick Trick: Use 'private' keyword on its own line before methods [OK]
Common Mistakes:
  • Trying to put 'private' inside method definition line
  • Using 'private def' which is invalid syntax
  • Placing 'private' after method definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes