Bird
0
0

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

easy📝 Syntax Q12 of 15
Ruby - Class Methods and Variables
Which of the following is the correct way to define a class method in Ruby?
Adef class.method_name; end
Bdef method_name; end
Cdef @method_name; end
Ddef self.method_name; end
Step-by-Step Solution
Solution:
  1. Step 1: Recall class method syntax

    In Ruby, class methods are defined with def self.method_name inside the class.
  2. Step 2: Check options

    def self.method_name; end uses def self.method_name, which is correct. def method_name; end defines an instance method. def @method_name; end uses invalid syntax. def class.method_name; end is incorrect syntax.
  3. Final Answer:

    def self.method_name; end -> Option D
  4. Quick Check:

    Class method syntax = def self.method_name [OK]
Quick Trick: Class methods start with self. inside class [OK]
Common Mistakes:
  • Defining class methods without self.
  • Using @ for method names
  • Trying def class.method_name syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes