Bird
0
0

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

easy📝 Syntax Q3 of 15
Ruby - Basics and Runtime
Which of the following is the correct way to define a method in Ruby?
Adef my_method() puts 'Hello' end
Bdef my_method; puts 'Hello'; end
Cmethod my_method() => puts 'Hello'
Dfunction my_method() { puts 'Hello' }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby method syntax

    Ruby methods start with 'def' and end with 'end'. The semicolon can separate statements on one line.
  2. Step 2: Check each option

    def my_method; puts 'Hello'; end uses correct syntax. function my_method() { puts 'Hello' } is JavaScript style. method my_method() => puts 'Hello' is invalid. def my_method() puts 'Hello' end is valid Ruby syntax if 'puts' and 'end' are on separate lines, but as written without newline or semicolon before 'end' is invalid.
  3. Final Answer:

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

    Ruby method syntax = def ... end [OK]
Quick Trick: Use 'def' and 'end' to define methods in Ruby [OK]
Common Mistakes:
  • Using JavaScript or other language syntax
  • Omitting 'end' keyword
  • Incorrect punctuation in method definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes