Bird
0
0

Which of the following is the correct way to define an instance method greet inside a Ruby class Person?

easy📝 Syntax Q12 of 15
Ruby - Classes and Objects

Which of the following is the correct way to define an instance method greet inside a Ruby class Person?

class Person
  ?
end
Adef self.greet; puts 'Hello'; end
Bdef greet; puts 'Hello'; end
Cdef Person.greet; puts 'Hello'; end
Ddef greet() puts 'Hello' end
Step-by-Step Solution
Solution:
  1. Step 1: Identify instance method syntax

    Instance methods are defined with def method_name inside the class without self..
  2. Step 2: Check options

    def greet; puts 'Hello'; end uses correct syntax. def self.greet; puts 'Hello'; end and def Person.greet; puts 'Hello'; end define class methods. def greet() puts 'Hello' end misses semicolons or line breaks, causing syntax issues.
  3. Final Answer:

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

    Instance method = def method_name [OK]
Quick Trick: Instance methods use def without self. [OK]
Common Mistakes:
  • Using self. which defines class methods
  • Omitting semicolons or line breaks incorrectly
  • Defining methods outside the class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes