Ruby - Blocks, Procs, and LambdasWhich of the following is the correct way to create a lambda in Ruby?A->(x) { x * 2 }Bdef lambda(x) x * 2 endClambda(x) => x * 2Dlambda(x) { x * 2 }Check Answer
Step-by-Step SolutionSolution:Step 1: Identify valid lambda syntaxIn Ruby, lambdas can be created using the arrow syntax ->(params) { code } or lambda { |params| code }.Step 2: Check each optionlambda(x) { x * 2 }: invalid syntax (lambda keyword expects block with |params|, not parenthesized args). def lambda(x) x * 2 end: method definition, not a lambda. lambda(x) => x * 2: invalid syntax. ->(x) { x * 2 }: correct arrow syntax.Final Answer:->(x) { x * 2 } -> Option AQuick Check:Arrow syntax ->(x) { ... } creates lambda [OK]Quick Trick: Use ->(params) { code } or lambda { |params| code } [OK]Common Mistakes:Using def instead of lambdaIncorrect arrow syntaxConfusing method and lambda syntax
Master "Blocks, Procs, and Lambdas" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Blocks, Procs, and Lambdas - Yield to execute blocks - Quiz 15hard Class Methods and Variables - Class instance variables as alternative - Quiz 5medium Classes and Objects - To_s method for string representation - Quiz 1easy Classes and Objects - Initialize method as constructor - Quiz 7medium Classes and Objects - Instance methods - Quiz 10hard Classes and Objects - Self keyword behavior - Quiz 4medium Inheritance - Accessing parent methods - Quiz 8hard Inheritance - Accessing parent methods - Quiz 6medium Modules and Mixins - Prepend for method chain insertion - Quiz 5medium Modules and Mixins - Custom modules as mixins - Quiz 10hard