0
0
Rubyprogramming~10 mins

Proc vs lambda differences (arity, return) in Ruby - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a lambda that takes one argument.

Ruby
my_lambda = [1] do |x|
  x * 2
end
Drag options to blanks, or click blank then click option'
Alambda
Bproc
CProc.new
Ddef
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'proc' instead of 'lambda' which does not enforce arity.
Using 'Proc.new' which creates a proc, not a lambda.
2fill in blank
medium

Complete the code to create a proc that ignores extra arguments.

Ruby
my_proc = [1] do |x|
  x + 1
end

puts my_proc.call(5, 10)
Drag options to blanks, or click blank then click option'
Alambda
Bdef
CProc.new
Dproc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lambda' which enforces exact number of arguments.
Using 'def' which defines a method, not a proc.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword to create a lambda.

Ruby
my_func = [1] do |x, y|
  return x + y
end

puts my_func.call(2, 3)
Drag options to blanks, or click blank then click option'
Alambda
BProc.new
Cproc
Ddef
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'proc' which causes 'return' to exit the enclosing method.
Using 'Proc.new' which behaves like 'proc'.
4fill in blank
hard

Fill both blanks to create a proc and call it with two arguments, ignoring extra ones.

Ruby
my_proc = [1] do |a, b|
  a - b
end

puts my_proc.call(10, 5, [2])
Drag options to blanks, or click blank then click option'
Aproc
Blambda
C20
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lambda' which enforces exact arguments and will raise an error.
Passing no extra argument which misses the point of ignoring extras.
5fill in blank
hard

Fill all three blanks to create a lambda, call it with correct arguments, and return the sum.

Ruby
my_lambda = [1] do |x, y|
  [2] x + y
end

result = my_lambda.call([3], 7)
puts result
Drag options to blanks, or click blank then click option'
Alambda
Breturn
C5
Dproc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'proc' which treats return differently.
Omitting 'return' which still works but is less explicit.