Bird
0
0

Which of the following is the correct way to define a method in Ruby that returns a greeting?

easy📝 Syntax Q3 of 15
Ruby - Basics and Runtime
Which of the following is the correct way to define a method in Ruby that returns a greeting?
Adef greet(name)\n 'Hello, ' + name + '!'\nend
Bfunction greet(name) { return 'Hello, ' + name + '!'; }
Cdef greet(name):\n return 'Hello, ' + name + '!'\nend
Ddef greet(name) { 'Hello, ' + name + '!' } end
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby method syntax

    Ruby methods start with def, followed by the method name and parameters, and end with end.
  2. Step 2: Check each option's syntax

    def greet(name)\n 'Hello, ' + name + '!'\nend uses correct Ruby syntax; B is JavaScript style; C uses Python-like colon; D uses braces incorrectly.
  3. Final Answer:

    def greet(name)\n 'Hello, ' + name + '!'\nend -> Option A
  4. Quick Check:

    Ruby method syntax = def ... end [OK]
Quick Trick: Ruby methods use def and end keywords [OK]
Common Mistakes:
MISTAKES
  • Using JavaScript or Python syntax in Ruby
  • Forgetting the end keyword
  • Using braces instead of end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes