Blocks, procs, lambdas

Contents
Methods that yield
Block params with &
Procs
Lambdas vs procs

Methods that yield

A method runs the attached block wherever it calls yield, pausing itself until the block finishes. A method may yield several times or not at all. The predicate block_given? reports whether a caller attached a block, so yielding stays safe.

def twice yield yield end twice do puts "hi" end

hi
hi

Block params with &

Prefix the last parameter with &block to capture the block as an ordinary object. Calling its call method runs the stored code, possibly much later. The same object can also travel onward into another method.

def run(&block) puts block.class block.call end run do puts "hello" end

Proc
hello

Procs

The method proc builds a block object that can sit in a variable and run later through call. Procs are lenient about arguments: missing ones arrive as nil, and extra ones are quietly ignored.

hi = proc { |name| puts "hi #{name}" } puts hi.class hi.call("Ada")

Proc
hi Ada

Lambdas vs procs

The method lambda builds a stricter cousin of a proc: a wrong argument count raises an ArgumentError at once. The predicate lambda? tells the two apart, returning true only for lambdas. Reach for lambdas when the stored code should behave like a strict function.

add = lambda { |a, b| puts a + b } add.call(2, 3) say = proc { |name| puts "hi #{name}" } puts add.lambda? puts say.lambda?

5
true
false

Next: String tools

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: