Ruby - Blocks, Procs, and LambdasWhy do closures in Ruby capture variables by reference rather than by value?ATo reflect the latest value of variables when the closure is calledBBecause Ruby does not support variable copyingCTo prevent memory leaksDBecause closures are immutable objectsCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand closure variable captureClosures capture variables by reference so they see the current value when called, not the value at creation.Step 2: Reason why this is usefulThis allows closures to reflect changes in variables made after closure creation.Final Answer:To reflect the latest value of variables when the closure is called -> Option AQuick Check:Closures capture by reference to track variable changes [OK]Quick Trick: Closures see variable changes by referencing variables [OK]Common Mistakes:Thinking closures copy variable values at creationBelieving Ruby lacks variable copyingConfusing immutability with variable capture
Master "Blocks, Procs, and Lambdas" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Blocks, Procs, and Lambdas - Proc creation and call - Quiz 14medium Class Methods and Variables - Open classes (reopening classes) - Quiz 5medium Enumerable and Collection Processing - Zip for combining arrays - Quiz 8hard Error Handling - Begin/rescue/end blocks - Quiz 1easy Error Handling - Raise for throwing errors - Quiz 15hard Error Handling - Raise for throwing errors - Quiz 5medium Error Handling - Ensure for cleanup - Quiz 7medium File IO - CSV library basics - Quiz 12easy Inheritance - Why single inheritance in Ruby - Quiz 6medium Inheritance - Subclass with < operator - Quiz 15hard