Bird
0
0

Which of the following is the correct syntax to define a class method named greet inside a Ruby class?

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

    Class methods in Ruby are defined by prefixing the method name with self. inside the class.
  2. Step 2: Check each option

    def self.greet; puts 'Hello'; end uses def self.greet, which is the correct syntax for a class method.
  3. Final Answer:

    def self.greet; puts 'Hello'; end -> Option C
  4. Quick Check:

    Class method syntax = def self.method_name [OK]
Quick Trick: Class methods always start with def self.method_name [OK]
Common Mistakes:
  • Omitting self. for class methods
  • Using instance variable syntax (@) for methods
  • Trying to use :: inside method definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes