Bird
0
0

How would you define a Ruby method named say_hello that returns the string 'Hello!' without using the return keyword?

easy📝 Syntax Q3 of 15
Ruby - Methods
How would you define a Ruby method named say_hello that returns the string 'Hello!' without using the return keyword?
Adef say_hello return 'Hello!' end
Bdef say_hello 'Hello!' end
Cdef say_hello puts 'Hello!' end
Ddef say_hello print 'Hello!' end
Step-by-Step Solution
Solution:
  1. Step 1: Define method without explicit return

    In Ruby, the last evaluated expression is returned automatically.
  2. Step 2: Use a string literal as the last expression

    Simply placing the string 'Hello!' as the last line returns it.
  3. Final Answer:

    def say_hello 'Hello!' end -> Option B
  4. Quick Check:

    Last expression is string literal [OK]
Quick Trick: Last expression is returned automatically [OK]
Common Mistakes:
  • Using puts or print which return nil
  • Forgetting that explicit return is optional
  • Assuming return keyword is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes