Bird
0
0

You want to define a Ruby method that returns a greeting with a name variable. Which style is best according to the Ruby style guide essentials?

hard📝 Application Q15 of 15
Ruby - Ecosystem and Best Practices
You want to define a Ruby method that returns a greeting with a name variable. Which style is best according to the Ruby style guide essentials?
def greetUser(name)
  return 'Hello, ' + name + '!'
end
Adef greetUser(name) "Hello, #{name}!" end
Bdef greet_user(name) "Hello, #{name}!" end
Cdef greet_user(name) 'Hello, ' + name + '!' end
Ddef greetUser(name) 'Hello, ' + name + '!' end
Step-by-Step Solution
Solution:
  1. Step 1: Apply method naming conventions

    Method names should use snake_case, so greet_user is correct.
  2. Step 2: Use string interpolation with double quotes

    Double quotes allow cleaner string interpolation instead of concatenation.
  3. Final Answer:

    def greet_user(name) "Hello, #{name}!" end -> Option B
  4. Quick Check:

    snake_case + interpolation = def greet_user(name) "Hello, #{name}!" end [OK]
Quick Trick: Use snake_case for methods and double quotes for interpolation [OK]
Common Mistakes:
  • Using CamelCase for method names
  • Using single quotes with interpolation expecting it to work
  • Concatenating strings instead of interpolation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes