0
0
Rubyprogramming~5 mins

Proc creation and call in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Proc in Ruby?
A Proc is an object that holds a block of code which can be stored in a variable and called later.
Click to reveal answer
beginner
How do you create a Proc in Ruby?
You create a Proc by using Proc.new { ... } or the shorthand proc { ... }.
Click to reveal answer
beginner
How do you call a Proc once it is created?
You call a Proc using proc_object.call(arguments).
Click to reveal answer
intermediate
What happens if you call a Proc with the wrong number of arguments?
A Proc is flexible and does not raise an error if called with fewer or more arguments than expected. Missing arguments become nil.
Click to reveal answer
intermediate
Difference between Proc and lambda in Ruby?
A Proc is more lenient with arguments and returns from the calling method, while a lambda checks arguments strictly and returns from itself.
Click to reveal answer
How do you create a Proc object in Ruby?
Aproc = function() { puts 'Hello' }
Bdef proc { puts 'Hello' }
Ccreate Proc { puts 'Hello' }
DProc.new { puts 'Hello' }
How do you execute the code inside a Proc named 'p'?
Ap.call()
Bexecute p()
Ccall p()
Dp.run()
What happens if you call a Proc with fewer arguments than it expects?
AMissing arguments become nil
BIt raises an error
CIt ignores extra arguments
DIt converts arguments to strings
Which of these is true about Procs compared to lambdas?
AProcs check argument count strictly
BProcs return from the calling method
CProcs cannot be stored in variables
DProcs are not objects
Which syntax creates a Proc using shorthand in Ruby?
Alambda { puts 'Hi' }
BProc.new do puts 'Hi' end
Cproc { puts 'Hi' }
Ddef proc; puts 'Hi'; end
Explain how to create and call a Proc in Ruby with an example.
Think about how you store a block of code and run it later.
You got /3 concepts.
    Describe the difference between a Proc and a lambda in Ruby.
    Focus on how they treat arguments and return statements.
    You got /3 concepts.