Bird
0
0

Which of the following is the correct way to declare a method named greet in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Methods
Which of the following is the correct way to declare a method named greet in Ruby?
Adef greet puts "Hello"
Bfunction greet() { puts "Hello" }
Cdef greet() puts "Hello" end
Dmethod greet() puts "Hello" end
Step-by-Step Solution
Solution:
  1. Step 1: Check Ruby method syntax

    Ruby methods start with def method_name() and end with end. Parentheses are optional but allowed.
  2. Step 2: Identify correct syntax

    def greet() puts "Hello" end uses def greet() puts "Hello" end, which is correct Ruby syntax. Options B and D use wrong keywords, and C misses end.
  3. Final Answer:

    def greet() puts "Hello" end -> Option C
  4. Quick Check:

    Correct Ruby method syntax = def greet() puts "Hello" end [OK]
Quick Trick: Use 'def name() ... end' to declare methods in Ruby [OK]
Common Mistakes:
MISTAKES
  • Forgetting the 'end' keyword
  • Using JavaScript style 'function' keyword
  • Missing parentheses or using wrong keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes