Bird
0
0

Given this method: ```ruby def config(options = {}) defaults = {host: 'localhost', port: 80} settings = defaults.merge(options) "Host: #{settings[:host]}, Port: #{settings[:port]}" end ``` What will be the output of `config(port: 3000)`?

hard📝 Application Q9 of 15
Ruby - Hashes
Given this method: ```ruby def config(options = {}) defaults = {host: 'localhost', port: 80} settings = defaults.merge(options) "Host: #{settings[:host]}, Port: #{settings[:port]}" end ``` What will be the output of `config(port: 3000)`?
AHost: , Port: 3000
BHost: localhost, Port: 80
CHost: localhost, Port: 3000
DError: merge method undefined
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash merging behavior

    defaults.merge(options) overwrites defaults with options keys.
  2. Step 2: Substitute merged values in output

    port is overwritten to 3000, host remains 'localhost'.
  3. Final Answer:

    Host: localhost, Port: 3000 -> Option C
  4. Quick Check:

    Hash merge overwrites keys from argument [OK]
Quick Trick: Use merge to override default hash values [OK]
Common Mistakes:
  • Expecting defaults to overwrite options
  • Confusing merge with merge!
  • Assuming error on merge call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes