Bird
0
0

Given this method:

hard📝 Application Q15 of 15
Ruby - Methods
Given this method:
def build_profile(name:, age:, city: "Unknown", **extras)
  profile = {name: name, age: age, city: city}
  profile.merge!(extras)
  profile
end

What will be the result of build_profile(name: "Bob", age: 25, hobby: "guitar", pet: "dog")?
A{:name=>"Bob", :age=>25, :city=>"Unknown", :hobby=>"guitar", :pet=>"dog"}
B{:name=>"Bob", :age=>25, :city=>"Unknown"}
CArgumentError: unknown keyword: hobby
D{:name=>"Bob", :age=>25, :city=>"Unknown", :extras=>{:hobby=>"guitar", :pet=>"dog"}}
Step-by-Step Solution
Solution:
  1. Step 1: Understand method parameters

    The method requires name and age, has optional city with default, and collects extra keyword arguments into extras.
  2. Step 2: Analyze method call

    The call passes name, age, and extra keywords hobby and pet. These extras are merged into the profile hash.
  3. Step 3: Determine final hash

    The returned hash includes all keys: name, age, default city, plus hobby and pet from extras.
  4. Final Answer:

    {:name=>"Bob", :age=>25, :city=>"Unknown", :hobby=>"guitar", :pet=>"dog"} -> Option A
  5. Quick Check:

    Extras merged into hash with defaults [OK]
Quick Trick: Use **extras to capture and merge extra keyword args [OK]
Common Mistakes:
MISTAKES
  • Expecting error on unknown keywords with **extras
  • Forgetting to merge extras into profile
  • Assuming extras stored under :extras key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes