Ruby - Blocks, Procs, and LambdasWhich of the following is a valid way to create a lambda in Ruby?Amy_lambda = ->(x) { x * 2 }Bmy_lambda = proc { |x| x * 2 }Cmy_lambda = function(x) { x * 2 }Dmy_lambda = lambda { |x| x * 2 }Check Answer
Step-by-Step SolutionSolution:Step 1: Identify valid lambda syntaxIn Ruby, lambdas can be created using lambda { |args| ... } or the arrow syntax -> my_lambda = ->(x) { x * 2 }Step 2: Check each optionmy_lambda = lambda { |x| x * 2 } uses lambda with block and arguments correctly. my_lambda = proc { |x| x * 2 } creates a proc, not a lambda. my_lambda = function(x) { x * 2 } uses invalid Ruby syntax. my_lambda = ->(x) { x * 2 } uses correct arrow lambda syntax with parentheses.Final Answer:my_lambda = lambda { |x| x * 2 } and my_lambda = ->(x) { x * 2 } are valid lambda definitions.Quick Check:Valid lambda syntax = A and D [OK]Quick Trick: Use lambda { |args| ... } or ->(args) { ... } [OK]Common Mistakes:Confusing proc with lambda syntaxOmitting parentheses in arrow syntaxUsing non-Ruby function 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