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:
Step 1: Recall Ruby method syntax
Ruby methods start with 'def' and end with 'end'. The semicolon can separate statements on one line.
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.
Final Answer:
def my_method; puts 'Hello'; end -> Option B
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
Master "Basics and Runtime" in Ruby
9 interactive learning modes - each teaches the same concept differently