DEV Community

Blocks, Procs, and the "&" Bridge (opens in new tab)

Discussed on DEV

&blk in a parameter list turns a block into a Proc; &proc at a call turns a Proc back into a block. 1. A block is syntax, not an object A block is the { ... } or do ... end you attach to a method call. It is not a value you can hold by itself. You can't store a block in a variable — Ruby won't even parse it: b = { |x| x * 2 } # SyntaxError # Ruby reads { } here as a hash, not a block. A method can carry at most one block: [1, 2, 3].each { |x| puts x } # fine — exactly one block # There is no ...

Read the original article
Sign in to keep reading the full article.

Keyboard Shortcuts

Navigation

Next / previous post
j/k
Open post
oorEnter
Preview post
v

Post Actions

Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Save / unsave
s

Recommendations

Add interest / feed
Enter
Not interested
x

Go to

Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Discover
gb
Search
/

General

Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help