Ruby - HashesWhich of the following method definitions correctly uses a hash as named parameters with a default empty hash?Adef greet(options = []) "Hello, #{options[:name]}!" endBdef greet(name: {}) "Hello, #{name}!" endCdef greet(options) "Hello, #{options.name}!" endDdef greet(options = {}) "Hello, #{options[:name]}!" endCheck Answer
Step-by-Step SolutionSolution:Step 1: Identify correct default parameter typeUsing options = {} sets default to an empty hash, allowing optional named parameters.Step 2: Check correct key access syntaxAccessing options[:name] is correct for hash keys.Final Answer:def greet(options = {})\n "Hello, #{options[:name]}!"\nend -> Option DQuick Check:Default empty hash = options = {} [OK]Quick Trick: Default hash parameter is options = {} [OK]Common Mistakes:MISTAKESUsing array [] instead of hash {} as defaultUsing keyword arguments syntax incorrectlyAccessing hash keys with dot notation
Master "Hashes" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Array sorting and reversing - Quiz 8hard Arrays - Accessing elements (indexing, first, last) - Quiz 5medium Arrays - Array slicing and ranges - Quiz 10hard Arrays - Array modification (push, pop, shift, unshift) - Quiz 11easy Control Flow - Why Ruby has multiple control flow styles - Quiz 8hard Hashes - Default values for missing keys - Quiz 9hard Hashes - Hash creation with symbols and strings - Quiz 12easy Methods - Method declaration with def - Quiz 5medium Ruby Basics and Runtime - Why Ruby emphasizes developer happiness - Quiz 6medium String Operations - String methods (upcase, downcase, strip) - Quiz 2easy