0
0
Rubyprogramming~5 mins

Lambda creation and behavior in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a lambda in Ruby?
A lambda is a special kind of anonymous function or block that can be stored in a variable and called later. It behaves like a method and can take arguments and return values.
Click to reveal answer
beginner
How do you create a lambda in Ruby?
You can create a lambda using either lambda { |args| ... } or ->(args) { ... } syntax.
Click to reveal answer
intermediate
What happens if you use return inside a lambda?
Using return inside a lambda exits only from the lambda itself, not from the surrounding method or block.
Click to reveal answer
intermediate
How does a lambda differ from a Proc in Ruby?
A lambda checks the number of arguments passed and treats return differently (returns from lambda only). A Proc does not check arguments strictly and return exits from the enclosing method.
Click to reveal answer
beginner
How do you call a lambda stored in a variable?
You call a lambda by using the .call method on the variable, like my_lambda.call(args).
Click to reveal answer
Which syntax creates a lambda in Ruby?
Aproc { |x| x * 2 }
B->(x) { x * 2 }
Cdef lambda(x); x * 2; end
Dfunction(x) { return x * 2 }
What happens if you pass the wrong number of arguments to a lambda?
AIt raises an ArgumentError.
BIt ignores extra arguments.
CIt sets missing arguments to nil.
DIt runs without error.
What does return inside a lambda do?
ARaises an error.
BReturns from the enclosing method.
CReturns from the lambda only.
DExits the program.
How do you execute a lambda stored in variable f?
Af.execute()
Bf.run()
Ccall f()
Df.call()
Which is true about lambdas compared to Procs?
ALambdas check arguments and Procs do not.
BProcs check arguments and Lambdas do not.
CBoth behave exactly the same.
DLambdas cannot be stored in variables.
Explain how to create and call a lambda in Ruby, including the syntax options.
Think about the arrow syntax and the lambda keyword.
You got /3 concepts.
    Describe the difference in behavior between a lambda and a Proc when using return and argument checking.
    Focus on how return and arguments behave differently.
    You got /4 concepts.