Bird
0
0

Which of the following correctly defines a class method named greet using self in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Classes and Objects
Which of the following correctly defines a class method named greet using self in Ruby?
Adef greet(self); 'Hello'; end
Bdef greet.self; 'Hello'; end
Cdef self.greet; 'Hello'; end
Ddef class.greet; 'Hello'; end
Step-by-Step Solution
Solution:
  1. Step 1: Syntax for class methods

    Class methods are defined by prefixing the method name with self. inside the class.
  2. Step 2: Analyze options

    def self.greet; 'Hello'; end uses def self.greet which is the correct syntax. Options B, C, and D are invalid Ruby syntax for class methods.
  3. Final Answer:

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

    Class methods use def self.method_name [OK]
Quick Trick: Class methods start with def self.method_name [OK]
Common Mistakes:
  • Using invalid syntax like def greet.self
  • Trying to pass self as a parameter
  • Using def class.method which is not valid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes