4. Identify the error in this Ruby method declaration:
def say_hello
puts "Hello"
medium
A. Missing 'end' keyword to close method
B. Missing parentheses after method name
C. Incorrect use of puts inside method
D. Method name cannot have underscore
Solution
Step 1: Check method structure
Ruby methods must start with def and end with end. This code misses the end keyword.
Step 2: Identify the missing part
Without end, Ruby will raise a syntax error because the method is not properly closed.
Final Answer:
Missing 'end' keyword to close method -> Option A
Quick Check:
Every method needs 'end' [OK]
Hint: Always close methods with 'end' keyword [OK]
Common Mistakes:
Forgetting 'end' after method body
Thinking parentheses are mandatory
Assuming underscores are invalid in names
5. You want to create a method greet_person that takes a name and returns a greeting like "Hello, Alice!". Which code correctly does this?
hard
A. def greet_person(name)
puts "Hello, #{name}!"
end
B. def greet_person(name)
"Hello, " + name + "!"
end
C. def greet_person(name)
return puts "Hello, " + name + "!"
end
D. def greet_person(name)
print "Hello, " + name + "!"
end
Solution
Step 1: Understand method return vs output
To return a greeting string, the method should produce the string as its last expression or use return. Using puts or print outputs to console but returns nil.
Step 2: Analyze each option
def greet_person(name)
"Hello, " + name + "!"
end returns the greeting string correctly. def greet_person(name)
puts "Hello, #{name}!"
end prints but returns nil. def greet_person(name)
return puts "Hello, " + name + "!"
end returns nil because puts returns nil. def greet_person(name)
print "Hello, " + name + "!"
end prints but does not return the string.
Final Answer:
def greet_person(name)
"Hello, " + name + "!"
end -> Option B
Quick Check:
Return string, not print it [OK]
Hint: Return string directly; puts prints but returns nil [OK]
Common Mistakes:
Using puts instead of returning string
Returning result of puts (which is nil)
Confusing print with return
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.