Bird
0
0

Which of the following is the correct syntax to define a method using a hash for named parameters in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Hashes
Which of the following is the correct syntax to define a method using a hash for named parameters in Ruby?
Adef greet(name: options = {})
Bdef greet(name, options = {})
Cdef greet(options: {})
Ddef greet(options = [])
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct method parameter syntax

    In Ruby, to accept named parameters as a hash, you define the method with a parameter defaulting to an empty hash, like options = {}.
  2. Step 2: Check each option

    def greet(name, options = {}) uses options = {} correctly. def greet(name: options = {}) uses invalid syntax. def greet(options: {}) tries to use keyword arguments incorrectly. def greet(options = []) uses an array instead of a hash.
  3. Final Answer:

    def greet(name, options = {}) -> Option B
  4. Quick Check:

    Hash parameter = options = {} [OK]
Quick Trick: Use options = {} to accept named parameters as a hash [OK]
Common Mistakes:
  • Using square brackets [] instead of curly braces {}
  • Confusing keyword arguments with hash parameters
  • Incorrect syntax like name: options = {}

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes