Ruby - HashesWhat happens if you assign a value to a hash key that already exists in Ruby?AThe new value is ignoredBAn error is raisedCThe old value is replaced with the new valueDBoth old and new values are stored in an arrayCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand hash key assignment behaviorAssigning a value to an existing key replaces the old value.Step 2: Confirm no error or array storage occursRuby hashes overwrite old values silently without error or storing multiple values.Final Answer:The old value is replaced with the new value -> Option CQuick Check:Hash key assignment replaces old value [OK]Quick Trick: Assigning existing key replaces old value in hash [OK]Common Mistakes:Expecting error on duplicate key assignmentThinking values accumulate in arraysAssuming new value is ignored
Master "Hashes" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Array sorting and reversing - Quiz 4medium Arrays - Array creation methods - Quiz 10hard Arrays - Array sorting and reversing - Quiz 11easy Loops and Iteration - Upto and downto methods - Quiz 11easy Loops and Iteration - Each as the primary iterator - Quiz 4medium Methods - Method naming conventions (? and ! suffixes) - Quiz 4medium String Operations - String concatenation and << - Quiz 15hard String Operations - String concatenation and << - Quiz 11easy Variables and Data Types - Nil as the absence of value - Quiz 1easy Variables and Data Types - Integer and Float number types - Quiz 15hard